Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

c# - Changing Theme in Windows 10 UWP App Programmatically

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.

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

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?

I'm using VS2015

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Updated answer with what I finally decided on.

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:

In the App.xaml.cs file:

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

    this.InitializeComponent();
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...