gpt4 book ai didi

c# - 如何加载外部 XAML ResourceDictionary

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:47 25 4
gpt4 key购买 nike

如何像这样动态添加 ResourceDictionary?

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

对于绝对 Uri,它可以完美地工作,但对于 Relative 则不能。

最佳答案

我使用 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);

对我来说效果很好。

关于c# - 如何加载外部 XAML ResourceDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9128287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com