How to let my WPF app respond to default app mode

Dorlig Na 1 Reputation point
2020-07-01T12:55:50.12+00:00

There is "choose your default app mode" in Win10 setting with two options, "Light" and "Dark". I know an UWP app automatically respond to the setting and change its appearance automatically. Now the Win32 common file open dialog, Window File Explorer also respond to this setting and change its appearance automatically. But how can my WPF app respond to this setting and change its appearance accordingly. How can I get the theme of this setting and get colors from it for different UI aspect, for example, button face color

BTW, I'm not talking about high contrast theme which can be set as current active theme, but "choose your default app mode" setting. I suppose they are different settings which are set separately.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,688 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2020-07-03T03:33:36.46+00:00

    WPF does not automatically get the default APP Mode, you can implement it yourself. The value of default app mode corresponds to the value of AppsUseLightTheme which is in Registry Key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize\AppsUseLightTheme. You can use the below code to get the value. In my Registry , the value of Light mode is 1.

     var v = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme", "1");  
    

    Then declare the themes referring to SystemColors

    public enum AppTheme  
    		{  
    			Light,  
    			Dark  
    		}  
    

    Then use it like below:

    this.Resources.MergedDictionaries[0].Source =new Uri($"/Themes/{appTheme}.xaml", UriKind.Relative);  
    
    0 comments No comments

  2. Ken Tucker 5,846 Reputation points
    2020-07-04T11:19:34.367+00:00

    Another way you can do it is by adding the NuGet package Microsoft.Windows.SDK.Contracts. Then you have access to the UISettings class which will allow to get the colors needed for the foreground and background.

        UISettings settings = new UISettings();
        var foreground = settings.GetColorValue(UIColorType.Foreground);
        var background = settings.GetColorValue(UIColorType.Background);
    

    There is an example here

    0 comments No comments