8

我想检查用户是否设置了浅色或深色主题。是否可以在 Windows Phone 8.1(商店应用程序)中以编程方式执行此操作。

4

2 回答 2

7

MSDN上,您会找到示例代码,您可以使用这些代码来确定当前主题 - 通过比较资源。例如:

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

但是 - 我在 WP8.1 运行时运行上述行时遇到了一些问题 - 它找不到请求的密钥。事实证明 - 上述代码仅适用于 WP8.1 Silverlight (also WP8.0)

但是(再次),在您定义自己的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 类或通过HighContrast 值扩展您自己创建的ThemeResource来完成。

于 2014-07-19T22:36:20.320 回答
5

要检查哪个主题处于活动状态,您可以使用 Application 对象MSDN的 RequestedTheme 属性

var isDark = Application.Current.RequestedTheme == ApplicationTheme.Dark;
于 2015-04-08T13:18:17.883 回答