对于 XAML Windows 商店应用程序,控件的默认外观在 Common/StandardStyles.xaml 中定义。如果您曾经查看过该文件,您会注意到大量引用,例如 {StaticResource ApplicationForegroundThemeBrush}。看起来很有希望……
很遗憾,这些“主题画笔”并未在您的应用中的任何地方定义,并且没有简单的方法可以为各个控件设置浅色或深色覆盖。但是,有一个答案。
幸运的是,there's an excellent blog post by Joe White 在默认主题颜色上,即I've turned into a resource dictionary that you can find here。 Dropbox 仅进行 xml 预览,因此您必须重命名文件。
不过,将这些资源复制到您的应用程序本身并无济于事。要使用它们,您需要通过手术覆盖默认控件样式才能使用它们!
一种方法是创建一个新的资源字典,例如Common/CustomStyles.xaml,并将其合并到应用程序的资源中,如下所示:
<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 并进行一些更改。
<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 附加到样式名称和主题画笔定义中!您可以通过在 CustomStyles.xaml 文件中查找并替换 "ThemeBrush}" => "ThemeBrushDark}" 来执行此操作.
现在您可以像这样创建一个深色主题的文本块:
<TextBlock Style="{StaticResource BasicRichTextStyleDark}"/>
当然,您也可以将此技术用于任何其他类型的控制。有点繁琐,但结果是正确的,而且还没有尝试手动定义每种颜色那么糟糕!
这里没有魔法。我们只是定义了一些颜色并用我们复制粘贴和修改以使用我们的颜色的一种覆盖默认样式。