如何加载外部XAML ResourceDictionary
如何像这样添加ResourceDictionary?
ResourceDictionary template = new ResourceDictionary(); template.Source = new Uri("Design.xaml", UriKind.Relative); Application.Current.Resources.MergedDictionaries.Add(template);
绝对的Uri它完美地工作但是来自Relative does。
我使用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);
对我来说效果很好。