【问题标题】:XamlReader throws when loading generic.xaml to merge resource dictionaries加载 generic.xaml 以合并资源字典时 XamlReader 抛出
【发布时间】:2011-12-09 23:52:38
【问题描述】:

尝试在代码中加载 generic.xaml,但会引发 XamlParseException。代码如下:

Uri uri = new Uri("Themes/Generic.xaml", UriKind.Relative);
StreamResourceInfo info = Application.GetResourceStream(uri);
System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();

ResourceDictionary resdict = (ResourceDictionary)reader.LoadAsync(info.Stream);

this.Resources.MergedDictionaries.Add(resdict);

这个想法是在一个基本页面中合并一个资源字典。然后派生页面可以使用其基类中的样式、颜色、画笔等,例如使用{StaticResource DarkBrush}

但是上面的代码抛出:

'',十六进制值 0x0C,是无效字符。第 1 行,位置 1。

generic.xaml 文件是在 VS2010 中以标准方式创建的。尝试将 Build Action 设置为 Resource 但这也不起作用...

我从 Microsoft 获得了 code sample。在那里它被用来加载一个页面。任何帮助将不胜感激。

【问题讨论】:

    标签: wpf xamlparseexception xamlreader generic.xaml


    【解决方案1】:
            Uri uri = new Uri("Themes/Generic.xaml", UriKind.RelativeOrAbsolute);
            var resDict = Application.LoadComponent(uri) as ResourceDictionary;
            this.Resources.MergedDictionaries.Add(resDict);
    

    【讨论】:

    • 谢谢!简单易行的解决方案!
    猜你喜欢
    • 2011-08-04
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多