我可以使用 this.RequestedTheme = ElementTheme.Dark; 更改主题
但是我需要的是整个应用程序级别,因为这个只是将当前页面的主题更改为深色。
每当我尝试这个 App.Current.RequestedTheme = ApplicationTheme.Dark;
我总是得到这个错误
An exception of type 'System.NotSupportedException' occurred in UWPApp.exe but was not handled in user code
有没有一种方法可以将整个应用程序主题从浅色更改为深色,反之亦然?
我正在使用 VS2015
最佳答案
更新了我最终决定的答案。
我使用了一个设置类来保存所有应用程序设置,包括要使用的主题。由于主题只能在启动时设置,我们需要确保设置它们。这是我使用的代码:
在 App.xaml.cs 文件中:
public App()
{
//Load settings
AppSettings.LoadSettings();
this.RequestedTheme = AppSettings.SelectedTheme;
this.InitializeComponent();
}
在 App.xaml 文件中确保删除此属性:
RequestedTheme="Light"
如果它没有被移除,它总是默认为亮,无法更改。
通过这种方式,用户可以选择主题,它会在应用启动时被存储和使用。只需确保在应用程序初始化阶段加载并应用它即可。
关于c# - 以编程方式更改 Windows 10 UWP App 中的主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34554871/