Don*_*ndo 7 resources xaml themes windows-10 uwp
我的问题非常特定于Windows 10商店应用程序中的ThemeResources.不幸的是,"经典"WPF中可用的一些东西在这里是不同的或不可用的.
我想要为许多ui元素实现的目标:
{ThemeResource SystemAccentColor}值.)SystemAccentColorresourcedictionary中的键)但我还没有找到一个很好的解决方案来实现这一切.如果我有自己的自定义颜色资源字典,当用户想要切换回系统的强调颜色时,我不会摆脱它.使用我绑定的属性有一个缺点,我没有意识到用户在应用程序运行时是否更改了系统设置中的重音颜色 - 使用{ThemeResource}标记它.
任何想法如何正确完成?如果可以设置ThemeResourcefrom代码,我可以为此编写一些行为,但它似乎不可用.
在 Windows 10 中,名称“Accent Color”更改为“SystemControlHighlightAccentBrush”,它是一个 ThemeResource
使用示例
<TextBlock Foreground="{ThemeResource SystemControlHighlightAccentBrush}"
Text="This is a sample text" />
Run Code Online (Sandbox Code Playgroud)
要覆盖它,只需在 App.xaml 中更改它的值
<Application.Resources>
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="Orange" />
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
要切换,稍微有点难度首先需要在App.xaml中设置每个主题的所有颜色
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="Orange" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="Green" />
</ResourceDictionary>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="Blue" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
然后,在页面或者后面的代码中,你设置相应的主题
<TextBlock x:Name="TestTextBlock"
Foreground="{ThemeResource SystemControlHighlightAccentBrush}"
RequestedTheme="Dark"
Text="This is a sample text" />
Run Code Online (Sandbox Code Playgroud)
或在 C# 中
TestTextBlock.RequestedTheme = ElementTheme.Dark;
Run Code Online (Sandbox Code Playgroud)
有一种方法可以ThemeResource在代码中进行设置...我仅在 W10 Creators Update 上对其进行了测试,因此它可能不适用于旧版本,但您可以创建自己的资源来引用ThemeResource您想要使用的原始资源,然后使用此资源:
XAML:
<SolidColorBrush x:Key="MyBorderBrush" Color="{ThemeResource SystemAccentColor}"/>
Run Code Online (Sandbox Code Playgroud)
C#:
element.BorderBrush = (SolidColorBrush)Resources["MyBorderBrush"];
Run Code Online (Sandbox Code Playgroud)
的边框颜色element将与 Windows 设置中选择的强调颜色相同,即使您的应用程序正在运行且用户更改它,它也会发生变化。