动态加载ResourceDictionary

Die*_*hon 4 c# wpf xaml resourcedictionary

我的项目中有一个文件夹,模板,完整的(已编译的)XAML ResourceDictionaries.

在UserControl中,我想将所有模板加载到ResourceDictionary中.我会使用如下代码:

public MyView()
{
    InitializeComponent();
    foreach (var resourceUri in new GetResourceUrisFromTemplatesFolder())
        Resources.MergedDictionaries.Add(
            new ResourceDictionary
                { Source = new Uri(resourceUri, UriKind.Relative) });
}
Run Code Online (Sandbox Code Playgroud)

我需要写的是GetResourceUrisFromTemplatesFolder方法.我需要它来发现该文件夹中的所有资源.

URI可以采用类似/MyAssembly;component/MyNS/Templates/Template12345.xaml或的形式../../Templates/Template12345.xaml

这可能吗?

我是否必须从程序集的已编译资源(MyAssembly.g.resources)手动转换名称?

Geo*_*lis 6

顺便说一句,人们也可以手动加载ResourceDictionary:

ResourceDictionary dict = new ResourceDictionary(); 
System.Windows.Application.LoadComponent(dict, 
  new System.Uri("/SomeAssembly;component/SomeResourceDictionary.xaml",
  System.UriKind.Relative));
Run Code Online (Sandbox Code Playgroud)

之后,可以使用它上面的Keys属性上的foreach与该dict对象进行通信等

http://msdn.microsoft.com/en-us/library/ms596995(v=vs.95).aspx

加载的XAML文件可以是应用程序定义文件(例如,App.xaml)>或页面文件(例如,MainPage.xaml).XAML文件可以位于以下位置之一:

  • 包含在应用程序包中.
  • 嵌入在应用程序组件中.
  • 嵌入在原始站点的库程序集中.