【发布时间】:2017-08-23 13:56:26
【问题描述】:
我有一个简单的 WPF 应用程序来更改颜色主题。
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,它存储三个枚举值“Blue、Green、Yellow”。这就是所选索引发生变化时的作用:
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,您甚至可以看到新的源代码。只有 UI 没有更新。
【问题讨论】:
-
您是如何引用 xaml 中的资源的?