Как загрузить внешний XAML ResourceDictionary - PullRequest
2 голосов
/ 03 февраля 2012

Как я могу добавить ResourceDictionary динамически, как это?

ResourceDictionary template = new ResourceDictionary();
template.Source = new Uri("Design.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(template);

С абсолютным Uri это работает отлично, но от Relative нет.

1 Ответ

1 голос
/ 24 февраля 2012

Я использую класс XamlReader для этого:

string exeFilePath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
string exeDirPath = Path.GetDirectoryName(exeFilePath);
string targetFile = "subfolder\\dictionary.xaml";
string path_to_xaml_dictionary = new Uri(Path.Combine(exeDirPath, targetFile)).LocalPath;
string strXaml = File.ReadAllText(path_to_xaml_dictionary);                    
ResourceDictionary resourceDictionary = (ResourceDictionary)XamlReader.Parse(strXaml);
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);

Хорошо работает для меня.

...