如何以编程方式检查

如何以编程方式检查

本文介绍了如何以编程方式检查 Windows Phone 8.1 中当前设置的主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to check if the user has set a light or dark theme. Is it possible to do so programmatically in Windows Phone 8.1 (store app).

推荐答案

此处位于 MSDN 您将找到示例代码,您可以使用这些代码来确定当前主题 - 通过比较资源.例如:

Here at MSDN you will find sample codes, which you can use to determine the current theme - by comparing resources. For example:

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

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

But - I've enocuntered some problems running the above line at WP8.1 Runtime - it couldn't find the requested key. As it turned out - the above code will work only on WP8.1 Silverlight (also WP8.0).

但是(再次),没有什么可以阻挡您的定义你自己的ThemeResource 并检查它的状态:

But (again), nothing stands on your way to define your own ThemeResource and check it's state:

在 app.xaml 中 - 定义一些 ThemeResources:

In app.xaml - define some 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>

然后您可以在代码中使用例如一个属性:

Then you can use for example a property in your code:

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

另请注意,在某些情况下,您可能需要检查 HighContrast - 根据 MSDN,你可以通过查看AccessibilitySettings 类 或通过 HighContrast 值扩展您自己创建的 ThemeResource.

Note also that in some cases you may need to check for HighContrast - according to MSDN, you can do it by checking AccessibilitySettings class or extend your own created ThemeResource by HighContrast values.

这篇关于如何以编程方式检查 Windows Phone 8.1 中当前设置的主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:03