简体   繁体   English

从已编译的DLL中获取所有XAML文件

[英]Get all XAML files from compiled DLL

I want to load during runtime external XAML styles from third-party libraries (DLLs). 我想在第三方库(DLL)的运行时外部XAML样式期间加载。 Like in this tutorial they use: 本教程中,他们使用:

Application.LoadComponent(new Uri("/WpfSkinSample;component/Skins/" + name + ".xaml", UriKind.Relative)) as ResourceDictionary;

to load the new style. 加载新样式。

But I don't know the XAML names from the third-party library, so I'm searching for a way to get them and load them into my application. 但是我不知道第三方库中的XAML名称,所以我正在寻找一种方法来获取它们并将它们加载到我的应用程序中。

Thanks for any help. 谢谢你的帮助。

Edit: Thanks to andyp, I did the following work out: 编辑:感谢andyp,我做了以下工作:

    public void LoadXaml(String Assemblypath)
    {
        var assembly = Assembly.LoadFile(Assemblypath);
        var stream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".g.resources");
        var resourceReader = new ResourceReader(stream);

        foreach (DictionaryEntry resource in resourceReader)
        {
            if (new FileInfo(resource.Key.ToString()).Extension.Equals(".baml"))
            {
                Uri uri = new Uri("/" + assembly.GetName().Name + ";component/" + resource.Key.ToString().Replace(".baml", ".xaml"), UriKind.Relative);
                ResourceDictionary skin = Application.LoadComponent(uri) as ResourceDictionary;
                this.Resources.MergedDictionaries.Add(skin);
            }
        }
    }

You can enumerate the embedded resources of an assembly like this: 您可以枚举程序集的嵌入资源,如下所示:

var assembly = Assembly.LoadFile("pathToYourAssembly");
var stream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".g.resources");
var resourceReader = new ResourceReader(stream);

foreach (DictionaryEntry resource in resourceReader)
    Console.WriteLine(resource.Key);

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

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