UWP Template10 - 更改主题时更改按钮背景颜色
UWP Template10 - Change button background color when changing theme
我在 UWP template10 项目中有许多按钮,我希望在切换到浅色或深色主题时自动更改按钮背景颜色。
我进入我的 custom.xaml,并添加了最后 5 行:
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="CustomColorBrush" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ContrastColorBrush" Color="{ThemeResource ContrastColor}" />
<SolidColorBrush x:Key="ExtendedSplashBackground" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ExtendedSplashForeground" Color="{ThemeResource ContrastColor}" />
<Style TargetType="controls:PageHeader">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
<!-- Change button background-->
<Style TargetType="Button">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
</ResourceDictionary>
....
但是这不起作用。谁能建议如何/在哪里做这件事?
如果您的 Styles 和 SolidColorBrushs 可以在您第一次 运行 应用程序时用于您的控件?如果不行,请尝试去掉App.xaml中的RequestedTheme="Dark"
。
此外,如果您使用 "Default" 作为键创建 ResourceDictionary,则当主题更改时它不会更改颜色。除了 "HighContrast" 词典之外,您还应该能够为 "Light" 和 "Dark" 指定主题词典。
例如:
在App.xaml中:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="CustomColor" Color="Orange" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="CustomColor" Color="Blue" />
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<SolidColorBrush x:Key="CustomColor" Color="Green" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
您还应该在 custom.xaml.
的词典中为 "Light"、"Dark" 和 "HighContrast" 添加指定主题词典
在custom.xaml中:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="CustomColorBrush" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ContrastColorBrush" Color="{ThemeResource ContrastColor}" />
<SolidColorBrush x:Key="ExtendedSplashBackground" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ExtendedSplashForeground" Color="{ThemeResource ContrastColor}" />
<Style TargetType="controls:PageHeader">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
<Style TargetType="Button">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="CustomColorBrush" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ContrastColorBrush" Color="{ThemeResource ContrastColor}" />
<SolidColorBrush x:Key="ExtendedSplashBackground" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ExtendedSplashForeground" Color="{ThemeResource ContrastColor}" />
<Style TargetType="controls:PageHeader">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
<Style TargetType="Button">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<SolidColorBrush x:Key="CustomColorBrush" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ContrastColorBrush" Color="{ThemeResource ContrastColor}" />
<SolidColorBrush x:Key="ExtendedSplashBackground" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ExtendedSplashForeground" Color="{ThemeResource ContrastColor}" />
<Style TargetType="controls:PageHeader">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
<Style TargetType="Button">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Page.Resources>
我在 UWP template10 项目中有许多按钮,我希望在切换到浅色或深色主题时自动更改按钮背景颜色。
我进入我的 custom.xaml,并添加了最后 5 行:
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="CustomColorBrush" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ContrastColorBrush" Color="{ThemeResource ContrastColor}" />
<SolidColorBrush x:Key="ExtendedSplashBackground" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ExtendedSplashForeground" Color="{ThemeResource ContrastColor}" />
<Style TargetType="controls:PageHeader">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
<!-- Change button background-->
<Style TargetType="Button">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
</ResourceDictionary>
....
但是这不起作用。谁能建议如何/在哪里做这件事?
如果您的 Styles 和 SolidColorBrushs 可以在您第一次 运行 应用程序时用于您的控件?如果不行,请尝试去掉App.xaml中的RequestedTheme="Dark"
。
此外,如果您使用 "Default" 作为键创建 ResourceDictionary,则当主题更改时它不会更改颜色。除了 "HighContrast" 词典之外,您还应该能够为 "Light" 和 "Dark" 指定主题词典。
例如:
在App.xaml中:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="CustomColor" Color="Orange" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="CustomColor" Color="Blue" />
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<SolidColorBrush x:Key="CustomColor" Color="Green" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
您还应该在 custom.xaml.
的词典中为 "Light"、"Dark" 和 "HighContrast" 添加指定主题词典在custom.xaml中:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="CustomColorBrush" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ContrastColorBrush" Color="{ThemeResource ContrastColor}" />
<SolidColorBrush x:Key="ExtendedSplashBackground" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ExtendedSplashForeground" Color="{ThemeResource ContrastColor}" />
<Style TargetType="controls:PageHeader">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
<Style TargetType="Button">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="CustomColorBrush" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ContrastColorBrush" Color="{ThemeResource ContrastColor}" />
<SolidColorBrush x:Key="ExtendedSplashBackground" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ExtendedSplashForeground" Color="{ThemeResource ContrastColor}" />
<Style TargetType="controls:PageHeader">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
<Style TargetType="Button">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<SolidColorBrush x:Key="CustomColorBrush" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ContrastColorBrush" Color="{ThemeResource ContrastColor}" />
<SolidColorBrush x:Key="ExtendedSplashBackground" Color="{ThemeResource CustomColor}" />
<SolidColorBrush x:Key="ExtendedSplashForeground" Color="{ThemeResource ContrastColor}" />
<Style TargetType="controls:PageHeader">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
<Style TargetType="Button">
<Setter Property="Background" Value="{ThemeResource CustomColorBrush}" />
<Setter Property="Foreground" Value="{ThemeResource ContrastColorBrush}" />
</Style>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Page.Resources>