我正在开发一个UWP应用程序,其中我想从文件中动态设置主题(包含颜色代码)。
我创建的文件是一个XML文件,其节点包含映射到应用程序控件的颜色代码。
用户可以在提供的XML文件中更新颜色代码,该颜色代码应反映应用程序中的主题更改。
我可以为此文件设置任何自定义位置,以便用户编辑文件的内容吗?
这是在UWP应用程序中实现主题的正确方法吗?
另外,我在UWP技术方面还很陌生。
提前致谢。
默认情况下,UWP应用程序支持两个主题-Light
和Dark
。
您可以在中App.xaml
通过将RequestedTheme
属性设置为值之一(Light
默认设置为该属性),或App.xaml.cs
在App构造函数中使用RequestedTheme = ApplicationTheme.Light;
(在以后将其更改为任何地方都会引发异常)来指定应用的主题。
如果未设置该RequestedTheme
属性,它将反映Settings > Personalization > Colors
在任何运行周年更新和更高版本的W10移动设备或W10 PC上设置的主题。在较旧的Windows 10桌面版本上,它将为Light
。
您还可以设置默认情况下FrameworkElement
设置为任何特定主题的主题,ElementTheme.Default
以便它从其父主题继承主题。
<StackPanel RequestedTheme="Light">
<TextBlock>Text using light theme.</TextBlock>
<TextBlock RequestedTheme="Dark">Text using dark theme.</TextBlock>
</StackPanel>
对于颜色自定义,UWP通常也使用用户指定的重音颜色Settings > Personalization > Colors
。
要反映在“设置”应用中设置的主题和强调色,并为某些元素指定自定义颜色,您必须使用ThemeResource
。您可以使用预定义的XAML主题的资源,例如此边框的背景颜色将#FFFFFFFF
在Light
主题#FF000000
中Dark
的主题。
<Border Background="{ThemeResource SystemControlBackgroundAltHighBrush}"/>
或者,您可以使用SystemControlBackgroundAccentBrush
它来反映在“设置”应用中选择的强调颜色。
您也可以编写自己的theme dictionary
字体,为每个主题指定颜色。这是一个简单的主题字典的示例:
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="MyButtonBackgroundThemeBrush" Color="White"/
<SolidColorBrush x:Key="MyButtonForegroundThemeBrush" Color="Black"/>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="MyButtonBackgroundThemeBrush" Color="Black"/>
<SolidColorBrush x:Key="MyButtonForegroundThemeBrush" Color="White"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
您将像这样使用它:
<Button Content="Themed button"
Background="{ThemeResource MyButtonBackgroundThemeBrush}"
Foreground="{ThemeResource MyButtonForegroundThemeBrush}"/
/>
按钮的背景将是White
和前景将是Black
在Light
主题,而Black
与White
在Dark
主题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句