如何使用WPF将不同的自定义主题(资源字典)应用到整个应用程序?
我想动态更改整个应用程序的自定义主题。主题以资源字典的形式呈现,称为 ExpressionDark.xaml 和 ExpressionLight.xaml(从 Codeplex 下载)。我使用组合框来选择适当的主题。主题更改发生在 SelectionChanged 事件中。这是代码:
private void themesComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ResourceDictionary resourceDictionary = new ResourceDictionary();
int theme = ((ComboBox)sender).SelectedIndex;
switch (theme)
{
case (int)Themes.Dark:
resourceDictionary = Application.LoadComponent(
new Uri(@"Themes\ExpressionDark.xaml",
UriKind.Relative)) as ResourceDictionary;
break;
case (int)Themes.Light:
resourceDictionary = Application.LoadComponent(
new Uri(@"Themes\ExpressionLight.xaml",
UriKind.Relative)) as ResourceDictionary;
break;
default:
break;
}
Application.Current.Resources = resourceDictionary;
}
这对于当前窗口工作正常,但是当我运行另一个应用程序窗口的实例时,会发生 XamlParseException。
I want to dynamically change custom theme for the whole application. Themes are presented as resource dictionaries called ExpressionDark.xaml and ExpressionLight.xaml (downloaded from Codeplex). I'm using combo box to select appropriate theme. Theme changing is happening at SelectionChanged event. Here's the code:
private void themesComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ResourceDictionary resourceDictionary = new ResourceDictionary();
int theme = ((ComboBox)sender).SelectedIndex;
switch (theme)
{
case (int)Themes.Dark:
resourceDictionary = Application.LoadComponent(
new Uri(@"Themes\ExpressionDark.xaml",
UriKind.Relative)) as ResourceDictionary;
break;
case (int)Themes.Light:
resourceDictionary = Application.LoadComponent(
new Uri(@"Themes\ExpressionLight.xaml",
UriKind.Relative)) as ResourceDictionary;
break;
default:
break;
}
Application.Current.Resources = resourceDictionary;
}
This works fine for the current window, but when I run an instance of another application window an XamlParseException occurs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)