且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式更改 Windows 10 UWP 应用程序中的主题

更新时间:2022-12-21 07:36:52

更新了我最终决定的答案.

我使用了一个设置类,其中包含所有应用设置,包括要使用的主题.由于主题只能在启动时设置,我们需要确保设置它们.这是我使用的代码:

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.