2017-02-13 211 views
1

我在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> 
.... 

然而,這是行不通的。任何人都可以建議如何/在哪裏做到這一點?

回答

1

如果您的樣式和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中,只是以前沒有看到它們。 :) –