2014-07-19 44 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; 
相关问题