WPF主题可以用于为可以在运行时更改的应用程序包含多个外观吗? [英] Can WPF themes be used to include multiple skins for an application that can be changed at runtime?

查看:94
本文介绍了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.

推荐答案

这是我在支持主题的应用程序中使用的代码段.在此示例中,我有两个主题(默认和经典XP).主题资源分别存储在DefaultTheme.xaml和ClassicTheme.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.

这是我的App.xaml中的默认代码

This is the default code in my App.xaml

<Application ...>
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ArtworkResources.xaml" />
                <ResourceDictionary Source="DefaultTheme.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="SwooshButton" TargetType="ButtonBase">
                <!-- style setters -->
            </Style>

            <!-- more global styles -->
        </ResourceDictionary>
    </Application.Resources>
</Application>

然后在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.

    private Themes _currentTheme = Themes.Default;
    public Themes CurrentTheme
    {
        get { return _currentTheme; }
        set { _currentTheme = value; }
    }

    public void ChangeTheme(Themes theme)
    {
        if (theme != _currentTheme)
        {
            _currentTheme = theme;
            switch (theme)
            {
                default:
                case Themes.Default:
                    this.Resources.MergedDictionaries.Clear();
                    AddResourceDictionary("ArtworkResources.xaml");
                    AddResourceDictionary("DefaultTheme.xaml");
                    break;
                case Themes.Classic:
                    this.Resources.MergedDictionaries.Clear();
                    AddResourceDictionary("ArtworkResources.xaml");
                    AddResourceDictionary("ClassicTheme.xaml");
                    break;
            }
        }
    }

    void AddResourceDictionary(string source)
    {
        ResourceDictionary resourceDictionary = Application.LoadComponent(new Uri(source, UriKind.Relative)) as ResourceDictionary;
        this.Resources.MergedDictionaries.Add(resourceDictionary);
    }

使用此方法还需要记住的是,任何利用主题的样式都需要具有动态资源.例如:

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:

<Window Background="{DynamicResource AppBackgroundColor}" />

这篇关于WPF主题可以用于为可以在运行时更改的应用程序包含多个外观吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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