如何在WPF中实现主题 [英] How to implement theming in wpf

查看:538
本文介绍了如何在WPF中实现主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WPF中实现ASP.Net类型的主题.用户可以选择各种预定义的主题,并可以切换到另一个主题.

I want to implement ASP.Net type theme in WPF. User have selection of various predefined themes and can switch to another theme.

为实现这一点,我尝试通过为每个主题创建不同的resourcedictionary并使用键存储所需的样式来进行尝试.在所有主题中,键的集合都是相同的,但是每个主题的定义都不同.

To implement this I have tried by creating different resourcedictionary for each theme and store required style with key. The collection of keys are same in all themes but its definition is different in each.

我正在使用以下代码在点击时切换主题:

I am using below code to switch theme on click:

var theme = Application.LoadComponent(uri) as ResourceDictionary;
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(theme);

在所有页面/用户控件/窗口中,我都设置了StaticResource并应用了适当的键. 当我切换到另一个主题时,它仅仅在UI上的下一个事件之后应用.

In all pages/ usercontrol/ windows I have set StaticResource and applied appropriate key. When I switch to another theme it is only applied after next event on UI.

如果我将StaticResource设置为DynamicResource,则将其立即应用.

If I set change StaticResource to DynamicResource then it is applied on immediately.

请分享您的想法,以通过StaticResource实现此目标?

Please share your idea to achieve this with StaticResource?

推荐答案

不幸的是,您无法使用StaticResource实现此目的.使用StaticResource的控件切换到另一个主题后需要重新加载以应用新资源.

Unfortunately, you can't achieve this with StaticResource. The Control with the StaticResource needs to be reloaded after switching to another Theme for the new resource to be applied.

您可能想要创建功能以在更改主题后重新加载当前页面.

You might want to create functionality to reload the current page after change of theme.

这篇关于如何在WPF中实现主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆