如何在 C#/XAML Windows 应用商店(Metro UI)应用程序中混合明暗主题? [英] How do I mix Light and Dark themes in a C#/XAML Windows Store (Metro UI) App?

查看:27
本文介绍了如何在 C#/XAML Windows 应用商店(Metro UI)应用程序中混合明暗主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 MSDN,设置应用主题的标准方法是在顶层 Application 上设置 RequestedTheme="Dark"RequestedTheme="Light" 实例.

According to MSDN, the standard way of setting your app theme is to set RequestedTheme="Dark" or RequestedTheme="Light" on the toplevel Application instance.

这适用于简单的应用程序,但很多时候我发现自己想要更改单个页面甚至单个控件的主题(例如,同一应用程序中的浅色主题文本编辑页面与深色主题图像查看器).

This works great for simple apps but many times I find myself wanting to change the theme of an individual page or even an individual control (e.g. a light-themed text edit page vs a dark-themed image viewer in the same app).

XAML 控件有 10 或 100 种视觉状态和颜色定义,手动设置它们中的每一种既乏味又难以 100% 正确!

XAML controls have 10s or 100s of visual states and color definitions and setting each one of them by hand is tedious and difficult to get 100% right!

是否有一种简单的方法可以在单个控件上设置深色或浅色主题?

Is there an easy way to set a dark or light theme on an individual control?

推荐答案

对于 XAML windows 商店应用程序,控件的默认外观在 Common/StandardStyles.xaml 中定义.如果你曾经看过那个文件,你会注意到大量的引用,比如 {StaticResource ApplicationForegroundThemeBrush}.看起来很有希望...

For a XAML windows store app, the default look-and-feel of controls is defined in Common/StandardStyles.xaml. If you've ever looked at that file, you'll notice a ton of references like {StaticResource ApplicationForegroundThemeBrush}. Looks promising...

遗憾的是,这些主题画笔"并未在您的应用中的任何位置定义,并且没有简单的方法可以为各个控件设置亮色或暗色覆盖.然而,有一个答案.

Unfortunately, these "theme brushes" aren't defined anywhere in your app, and there's no easy way to set a light or dark override for individual controls. However, there is an answer.

幸运的是,Joe White 有一篇关于默认主题颜色的精彩博文我已经变成了一个资源字典,你可以在这里找到.Dropbox 只进行 xml 预览,因此您必须重命名文件.

Fortunately, there's an excellent blog post by Joe White on the default theme colors, which I've turned into a resource dictionary that you can find here. Dropbox only does xml previews so you'll have to rename the file.

不过,将这些资源复制到您的应用本身无济于事.要使用它们,您需要修改默认控件样式才能使用它们!

Copying these resources to your app won't help by itself though. To make use of them, you need to surgically override the default control styles to use them!

一种方法是创建一个新的资源字典,例如在 Common/CustomStyles.xaml,并将其合并到应用程序的资源中,如下所示:

One way to do this is to create a new resource dictionary, e.g. at Common/CustomStyles.xaml, and merge that into the app's resources like so:

<Application
    x:Class="My.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    RequestedTheme="Light">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>

                <!-- 
                    Styles that define common aspects of the platform look and feel
                    Required by Visual Studio project and item templates
                 -->
                <ResourceDictionary Source="Common/ThemeColors.xaml"/>
                <ResourceDictionary Source="Common/StandardStyles.xaml"/>
                <ResourceDictionary Source="Common/CustomStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

请注意,我们的默认主题是 Light.如果我们想制作一个 Dark 主题的 TextBlock,让我们从 StandardStyles.xaml 复制视觉样式并将其添加到我们的 CustomStyles.xaml 并进行一些更改.

Notice that our default theme is Light. If we'd like to make a Dark-themed TextBlock, let's copy the visual style from StandardStyles.xaml and add it to our CustomStyles.xaml and make a few changes.

<Style x:Key="BasicRichTextStyleDark" TargetType="RichTextBlock">
    <Setter Property="Foreground" Value="{StaticResource ApplicationForegroundThemeBrushDark}"/>
    <Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/>
    <Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/>
    <Setter Property="TextTrimming" Value="WordEllipsis"/>
    <Setter Property="TextWrapping" Value="Wrap"/>
    <Setter Property="Typography.StylisticSet20" Value="True"/>
    <Setter Property="Typography.DiscretionaryLigatures" Value="True"/>
    <Setter Property="Typography.CaseSensitiveForms" Value="True"/>
</Style>

请注意,我们已将 Dark 附加到样式名称和主题画笔定义! 您可以通过查找和替换 "ThemeBrush}" 来完成此操作.code> => "ThemeBrushDark}" 在您的 CustomStyles.xaml 文件中.

Notice that we have appended Dark to the style name and the theme brush definitions! You can do this via find and replace "ThemeBrush}" => "ThemeBrushDark}" in your CustomStyles.xaml file.

现在您可以像这样创建一个深色主题的文本块:

Now you can create a dark-themed text block like so:

<TextBlock Style="{StaticResource BasicRichTextStyleDark}"/>

当然,您也可以将此技术用于任何其他类型的控件.有点乏味,但结果是正确的,而且还没有尝试手动定义每种颜色那么糟糕!

Of course, you can use this technique for any other type of control as well. A little tedious, but the results are correct and it's not nearly as bad as trying to define every color manually!

这里没有魔法.我们只是定义了一些颜色并使用我们复制粘贴和修改以使用我们的颜色的默认样式覆盖.

There's no magic here. We're just defining some colors and overriding a default style with one that we copy-pasted and modified to use our colors.

这篇关于如何在 C#/XAML Windows 应用商店(Metro UI)应用程序中混合明暗主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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