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

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

问题描述

我可以使用 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 应用程序中的主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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