WPF 主题可以用于包含可在运行时更改的应用程序的多个外观吗?
WPF 允许控件库为不同的系统主题提供不同的资源字典,本质上允许应用程序匹配操作系统选择的视觉主题(Aero、Luna 等)。
我想知道是否可以在我的应用程序中包含多个主题资源字典,并利用框架内的一些现有主题支持。 这应该适用于我自己的主题名称,并且理想情况下允许用户更改主题,从而更改应用程序在运行时的皮肤外观。 即使这只是一个配置设置,它仍然可能很有趣。
WPF allows a control library to provide different resource dictionaries for different system themes, essentially allowing an application to match the operating system's selected visual theme (Aero, Luna, etc).
I'm wondering if I can include multiple theme resource dictionaries with my application and utilise some existing theme support within the framework. This should work for my own theme names, and ideally allow the user to change the theme and therefore the skinned appearance of the application at runtime. Even if this were only a config setting, it could still be interesting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我在支持主题的应用程序中使用的一段代码。 在此示例中,我有两个主题(默认和经典 XP)。 主题资源分别存储在DefaultTheme.xaml和ClassicTheme.xaml中。
这是我的 App.xaml 中的默认代码
然后在 App.xaml 后面的代码中我有以下方法来允许更改主题。 基本上,您要做的就是清除资源字典,然后使用新主题重新加载字典。
使用这种方法还需要记住的是,任何使用主题的样式都需要有动态资源。 例如:
Here is a snippet of code that I used in my application that supported theming. In this example, I have two themes (Default and Classic XP). The theme resources are stored in the DefaultTheme.xaml and ClassicTheme.xaml respectively.
This is the default code in my App.xaml
Then in the code behind of the App.xaml I have the following method to allow for changing the theme. Basically, what you do is clear the Resource Dictionaries and then reload the dictionary with the new theme.
What you'll also need to keep in mind with this approach is that any styles that utilize a theme will need to have a dynamic resource. For example:
我不知道在框架中执行此操作的方法,但如果您为每个可以更改自己的控件设置样式,则可以做到这一点。
其原理是将样式设为
DynamicResource
,然后根据用户对不同样式的配置加载ResourcesDictionary
。这里是一篇包含示例的文章。
I don't know of a way of doing this in the framework, but you can do it if you style every control that can change yourself.
The theory is to make the style a
DynamicResource
and then load theResourcesDictionary
based on the users configuration for the different style.Here is an article that has an example.