2014-07-19 92 views

回答

7

在這裏MSDN您可以找到示例代碼,您可以通過比較資源來確定當前主題。例如:

private bool IsDarkTheme() 
{ return (double)Application.Current.Resources["PhoneDarkThemeOpacity"] > 0; } 

但是 - 我已經在WP8.1 Runtime中運行上面的一行代碼時遇到了一些問題 - 它找不到請求的密鑰。事實證明 - 上述代碼將工作only on WP8.1 Silverlight (also WP8.0)

但(再次),沒有站在你的方式to define your own ThemeResource並檢查它的狀態:

在App.xaml中 - 定義一些ThemeResources

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.ThemeDictionaries> 
      <ResourceDictionary x:Key="Light"> 
       <x:Boolean x:Key="IsDarkTheme">false</x:Boolean> 
      </ResourceDictionary> 
      <ResourceDictionary x:Key="Dark"> 
       <x:Boolean x:Key="IsDarkTheme">true</x:Boolean> 
      </ResourceDictionary> 
      <ResourceDictionary x:Key="Default"> 
       <x:Boolean x:Key="IsDarkTheme">false</x:Boolean> 
      </ResourceDictionary> 
     </ResourceDictionary.ThemeDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

然後你可以使用例如財產在你的代碼:

public bool IsDarkTheme { get { return (bool)Application.Current.Resources["IsDarkTheme"]; } } 

還要注意的是,在某些情況下,你可能需要檢查HighContrast - 根據MSDN,您可以通過檢查AccessibilitySettings class或擴展您自己創建的ThemeResource高對比度值

3

要檢查哪些主題是積極的,你可以使用Application對象的RequestedTheme財產MSDN

var isDark = Application.Current.RequestedTheme == ApplicationTheme.Dark; 
相關問題