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

标签 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);
            }
        }
    }

最佳答案

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

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

关于c# - 从已编译的 DLL 中获取所有 XAML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22747305/

相关文章:

c# - 如何在xaml中连接按钮内容?

c# - 如何拉伸(stretch)/最大化 AvalonDock DocumentPane

c# - 从 WPF 中的不同线程更新 UI 控件时出现 "The calling thread cannot access this object because a different thread owns it"错误

wpf - 如何将大型 XAML 文件分解为子 XAML 文件并保持父对象和子对象之间的通信?

c# - Winforms 设计器错误 "An error occured while parsing EntityName"

c# - 在 parse.com 中一次保存多个对象

wpf - DataGrid行详细信息可见性

wpf - 如何在XAML中按颜色组件定义Silverlight颜色?

c# - 自安装使用参数执行的服务

c# - C# 中使用最广泛的日志记录框架是什么?