我有一个应用程序,允许用户根据外部 xaml 文件更改配色方案。当用户单击包含主题名称的 MenuItem 时,将执行以下代码:
MenuItem mi = sender as MenuItem;
string executingDirectory = Assembly.GetExecutingAssembly().Location.Substring(0, Assembly.GetExecutingAssembly().Location.LastIndexOf("\\"));
string path = System.IO.Path.Combine(executingDirectory + "\\Themes", mi.Header.ToString() + ".xaml");
if (File.Exists(path))
{
using (FileStream fs = new FileStream(path, FileMode.Open))
{
ResourceDictionary dic = (ResourceDictionary)XamlReader.Load(fs);
Resources.MergedDictionaries.Clear();
Resources.MergedDictionaries.Add(dic);
}
}
这适用于大多数应用程序——我所有的资源画笔都发生了变化——但有一个例外。我有一个子控件,其背景颜色由使用转换器的值绑定确定。不过,我没有将颜色硬编码到转换器中,而是让转换器使用字符串常量作为画笔名称,然后从 App.Current.Resources 返回颜色:
Brush ret = Application.Current.Resources[brushToReturn] as Brush;
这里似乎发生的是 Application.Current.Resources 没有与窗口拥有相同的资源集。我已经尝试将主题加载到 Application.Current.Resources 并从转换器中读取,但这似乎也不起作用。谁能告诉我我在这里缺少什么?有没有办法改变 Application.Current.Resources 并影响打开的窗口?
谢谢!