将多个资源字典动态添加到我的WPF项目中

时间:2017-07-18 07:01:40

标签: c# wpf xaml dictionary

我需要一些WPF资源字典的帮助。我有多个.dll,并且在每个dll中都有一个或多个资源字典。问题是我不知道DLL中的资源字典的名称或位置。现在我正在寻找一种方法在我的Main.xaml

中的代码隐藏中添加所有这些资源字典

也许在装配中有反射的东西?

ResourceDictionary resourceDictionary = new ResourceDictionary();
resourceDictionary.Source = new Uri("Dictionary1.xaml", UriKind.Relative);

ResourceDictionary resourceDictionary2 = new ResourceDictionary();
resourceDictionary.Source = new Uri("Dictionary2.xaml", UriKind.Relative);

resourceDictionary2.MergedDictionaries.Add(resourceDictionary);

Resources.MergedDictionaries.Add(resourceDictionary2);

但它只添加1个资源字典......你能帮助我吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

ResourceDictionary resourceDictionary = new ResourceDictionary
{
     Source = new Uri("Dictionary1.xaml", UriKind.Relative)
};

ResourceDictionary resourceDictionary2 = new ResourceDictionary
{
     Source = new Uri("Dictionary2.xaml", UriKind.Relative)
};

Application.Current.Resources.MergedDictionaries.Add(resourceDictionary );
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary2 );

答案 1 :(得分:1)

我认为你的逻辑错了。尝试

//Load #1 ResourceDictionary
ResourceDictionary resourceDictionary = new ResourceDictionary();
resourceDictionary.Source = new Uri("Dictionary1.xaml", UriKind.Relative);

//Load #2 ResourceDictionary
ResourceDictionary resourceDictionary2 = new ResourceDictionary();
resourceDictionary2.Source = new Uri("Dictionary2.xaml", UriKind.Relative);

//Merge #1 & #2 
resourceDictionary2.MergedDictionaries.Add(resourceDictionary);

//Add to Resources
Resources.MergedDictionaries.Add(resourceDictionary2);