UWP Template10 - 更改主题时更改按钮背景颜色

问题描述:

我在UWP template10项目中有多个按钮,并且当我切换到浅色或深色主题时,我正在寻找自动更改按钮背景色。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> 
.... 

然而,这是行不通的。任何人都可以建议如何/在哪里做到这一点?

如果您的样式和SolidColorBrushs可以在您首次运行应用程序时用于您的控件?如果没有,请尝试删除App.xaml中的RequestedTheme="Dark"

此外,如果使用“默认”作为关键字创建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。

在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> 
+0

谢谢杰登的时间。你的回答确实让我走上了正确的道路,以纠正这个问题。 名称(默认)直接来自template10,所以也许应该修改模板以反映Dark而不是Default?然后,颜色已经设置在custom.xaml的开头,所以不需要将它们添加到app.xaml中,只是以前没有看到它们。 :) –