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

时间:2014-03-30 17:32:40

标签: c# wpf xaml

我想在第三方库(DLL)的运行时外部XAML样式期间加载。就像他们使用的this tutorial一样:

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

加载新样式。

但是我不知道第三方库中的XAML名称,所以我正在寻找一种方法来获取它们并将它们加载到我的应用程序中。

感谢您的帮助。

修改 感谢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);
            }
        }
    }

1 个答案:

答案 0 :(得分:10)

您可以枚举这样的程序集的嵌入资源:

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);