C# UWP应用程序中的自定义主题
我正在开发一个UWP应用程序,其中我希望从一个文件(包含颜色代码)动态设置主题 我创建的文件是一个XML文件,其节点包含映射到应用程序控件的颜色代码 用户可以更新提供的XML文件中的颜色代码,这应该反映应用程序中的主题更改 我可以为该文件设置任何自定义位置,以便用户可以编辑该文件的内容吗 这是在UWP应用程序中实现主题的正确方法吗 另外,我对超宽带技术很新C# UWP应用程序中的自定义主题,c#,themes,uwp,C#,Themes,Uwp,我正在开发一个UWP应用程序,其中我希望从一个文件(包含颜色代码)动态设置主题 我创建的文件是一个XML文件,其节点包含映射到应用程序控件的颜色代码 用户可以更新提供的XML文件中的颜色代码,这应该反映应用程序中的主题更改 我可以为该文件设置任何自定义位置,以便用户可以编辑该文件的内容吗 这是在UWP应用程序中实现主题的正确方法吗 另外,我对超宽带技术很新 提前感谢。默认情况下,UWP应用程序支持两个主题—亮和暗 您可以在app.xaml中指定应用程序的主题,方法是将RequestedTheme
提前感谢。默认情况下,UWP应用程序支持两个主题—
亮
和暗
您可以在app.xaml
中指定应用程序的主题,方法是将RequestedTheme
属性设置为其中一个值(默认设置为Light
),或在app.xaml.cs
中使用RequestedTheme=ApplicationTheme.Light代码>(稍后在任何位置更改它将导致引发异常)
如果您没有设置RequestedTheme
属性,它将在任何运行周年更新和更新版本的W10移动设备或W10 PC上反映Settings>Personalization>Colors
中设置的主题。在较旧的Windows 10桌面版本上,它将是灯
您还可以设置任何特定FrameworkElement
的主题,该元素默认设置为ElementTheme.Default
,以便从其父元素继承主题
<StackPanel RequestedTheme="Light">
<TextBlock>Text using light theme.</TextBlock>
<TextBlock RequestedTheme="Dark">Text using dark theme.</TextBlock>
</StackPanel>
或者您可以使用SystemControlBackgroundAccentBrush
,它将反映在“设置”应用程序中选择的重音颜色
您还可以编写自己的主题词典
,为每个主题指定颜色。下面是一个简单主题词典的示例:
<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>
<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}"/
/>