以编程方式更改Windows 10 UWP App中的主题 [英] Changing Theme in Windows 10 UWP App Programmatically

查看:202
本文介绍了以编程方式更改Windows 10 UWP App中的主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用 this.RequestedTheme = ElementTheme.Dark;
来更改主题,但是我需要的是整个应用程序级别,因为这只是一个更改

I was able to change theme using this.RequestedTheme = ElementTheme.Dark; But what I need is the whole application level, since this one only change the theme of the current page to dark.

每当我尝试使用此 App.Current.RequestedTheme = ApplicationTheme.Dark;
我总是会收到此错误

Whenever I try this App.Current.RequestedTheme = ApplicationTheme.Dark; I always get this error


UWPApp.exe中发生类型为'System.NotSupportedException'的异常,但未处理在用户代码中

An exception of type 'System.NotSupportedException' occurred in UWPApp.exe but was not handled in user code

是否可以通过这种方式将整个应用程序主题从浅色更改为深色,反之亦然?

Is there such a way that I can change the whole application theme from Light to Dark or vice versa?

我正在使用VS2015

I'm using VS2015

推荐答案

我用最新答案终于决定了。

我使用了一个设置类,该类包含所有应用程序设置,包括要使用的主题。由于主题只能在启动时设置,因此我们需要确保对其进行设置。这是我使用的代码:

I used a settings class that holds all of the apps settings including what theme to use. Since the theme can only be set when it starts we need to make sure to set it them. This is the code I used:

在App.xaml.cs文件中:

In the App.xaml.cs file:

public App()
{
    //Load settings
    AppSettings.LoadSettings();
    this.RequestedTheme = AppSettings.SelectedTheme;

    this.InitializeComponent();
}

在App.xaml文件中,请确保删除此属性:

In the App.xaml file make sure to remove this property:

    RequestedTheme="Light"

如果未将其删除,则始终默认为亮,无法更改它。

If its not removed it always default to light with no way to change it.

这种方式,用户可以选择主题,它会在应用启动时被存储和使用。只要确保将其加载并应用到应用程序初始化阶段即可。

This way the user can choose the theme, it gets stored and used when the app starts. Just make sure to load it and apply it in the app initialization phase.

这篇关于以编程方式更改Windows 10 UWP App中的主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆