简体   繁体   English

使用XamlReader.Load()解析嵌套的xaml文件

[英]Parsing nested xaml files with XamlReader.Load()

I'm doing a XamlReader.Load method on a resource dictionary, which has a couple of merged dictionaries. 我正在资源字典上执行XamlReader.Load方法,该方法具有几个合并的字典。

FileStream s = new FileStream(@"/Resources/Xaml/MainXaml.xaml", FileMode.Open);
var obj = XamlReader.Load(s);

MainXaml then loads a few other xaml files: 然后MainXaml加载其他一些xaml文件:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="first.xaml"></ResourceDictionary>
        <ResourceDictionary Source="second.xaml"></ResourceDictionary>       
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Whenever I try to do this, I have get an exception: 每当我尝试执行此操作时,都会出现异常:

'Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' “设置属性'System.Windows.ResourceDictionary.Source'引发了异常。” Line number '4' and line position '29'. 行号“ 4”和行位置“ 29”。 ---> System.IO.IOException: Assembly.GetEntryAssembly() returns null. ---> System.IO.IOException:Assembly.GetEntryAssembly()返回null。

Set the Application.ResourceAssembly property or use the pack://application:,,,assemblyname;component/ syntax to specify the assembly to load the resource from. 设置Application.ResourceAssembly属性或使​​用pack:// application:,, assemblyname; component /语法指定要从中加载资源的程序集。

Even when I try including the assembly name in the Source, the error persits 即使我尝试在源代码中包含程序集名称,错误仍然存​​在

<ResourceDictionary Source="/MyApp;first.xaml"></ResourceDictionary>

Use Pack Uri's 使用Pack Uri的

<ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Assembly_Name;component/first.xaml"/>
        </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Or 要么

 FileStream s = new FileStream(@"pack://application:,,,/Assembly_Name;component/Xaml/MainXaml.xaml", FileMode.Open);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM