我有一个简单的WPF应用程序来更改颜色主题。WPF Color Theme
ResourceDictionary blueDict = new ResourceDictionary() { Source = new Uri(@"/Styles/Colors/Blue/BlueColors.xaml", UriKind.Relative) };
ResourceDictionary greenDict = new ResourceDictionary() { Source = new Uri(@"/Styles/Colors/Green/GreenColors.xaml", UriKind.Relative) };
ResourceDictionary yellowDict = new ResourceDictionary() { Source = new Uri(@"/Styles/Colors/Yellow/YellowColors.xaml", UriKind.Relative) };
ResourceDictionary genericDict = new ResourceDictionary() { Source = new Uri(@"/Styles/Colors/GenericColors.xaml", UriKind.Relative) };
在MainWindow上我有一个ComboBox存储三个枚举值“蓝色,绿色,黄色”。这就是它的时候所选择的指标发生了变化:
Application.Current.Resources.MergedDictionaries.Clear();
Themes newTheme = (Themes)cbxThemes.SelectedItem;
if (newTheme == currentTheme)
return;
switch (newTheme)
{
case Themes.Blue:
Application.Current.Resources.MergedDictionaries.Add(blueDict);
break;
case Themes.Green:
Application.Current.Resources.MergedDictionaries.Add(greenDict);
break;
case Themes.Yellow:
Application.Current.Resources.MergedDictionaries.Add(yellowDict);
break;
default:
break;
}
Application.Current.Resources.MergedDictionaries.Add(genericDict);
currentTheme = newTheme;
第一次,一切正常,我可以选择我想要的颜色,但是当我再次改变颜色,没有任何反应。
有没有什么不在后台更新?
该代码有效,如果输出Application.Current.Resources.MergedDictionaries
,您甚至可以看到新的来源。只有用户界面不会更新。
你是如何引用xaml中的资源的? – MikeT
请参阅https://social.msdn.microsoft.com/Forums/vstudio/en-US/44e80e86-9c62-4b77-87bc-00997a4796f4/change-in-applicationcurrentresourcesmergeddictionaries-is-n--visually-updating?forum=wpf – MikeT