14

I'm using Windows.UI.ViewManagement.UISettings to get system accent color but it seems this class does not have any method or property for light/dark mode. I failed to find a document for this feature, how can I detect this?

PS: I'm making a JS app which does not have access for Windows.UI.Xaml namespace.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
Kagami Sascha Rosylight
  • 1,412
  • 1
  • 14
  • 31

7 Answers7

18

You can create a Windows Runtime Component project in your solution from there you access Windows.UI.Xaml namespace. Add a method to check current ApplicationTheme like that.

public sealed class Test
{
    public static string CurrentTheme()
    {
        var isDark = Application.Current.RequestedTheme == ApplicationTheme.Dark;

        if (isDark)
            return "Dark";

        return "Light";
    }
}

Add reference to windows runtime component project in your javascript app project and you can call this method where ever you want to check application theme. Take a look here for walkthrough on createing Windows Runtime Component.

Aman Sharma
  • 698
  • 7
  • 17
16

I have found an easier solution, which should work in JavaScript apps as well, without requiring the Windows Runtime Component - the UISettings class:

var uiSettings = new Windows.UI.ViewManagement.UISettings();
var color = uiSettings.getColorValue(
                        Windows.UI.ViewManagement.UIColorType.background
                       );

The color you get is either black for dark theme or white for light theme.

The class also has very useful event ColorValuesChanged which you can use to observe theme changes at runtime.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
14

For Windows 10, the value of the AppsUseLightTheme property in the path HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize of the registry specifies wherever Windows is in dark or light mode.

Mike
  • 14,010
  • 29
  • 101
  • 161
  • 6
    AppsUseLightTheme (REG_DWORD): 0 = Dark mode, 1 = Light mode – Matt Mar 31 '20 at 11:05
  • 2
    Be aware that the `AppsUseLightTheme` value might be missing completely. In this case, you should fall back to a light mode. (Happened to me on an older Windows 10 system; the one that comes with the IE Test VM images from Microsoft itself). – Uwe Keim Sep 04 '20 at 08:11
4

You can read the registry if the theme is enabled for the current user and get the setting from there. Something like this...

private const string RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";

private const string RegistryValueName = "AppsUseLightTheme";

private static ApplicationTheme GetWindowsTheme()
{
    using var key = Registry.CurrentUser.OpenSubKey(RegistryKeyPath);
    var registryValueObject = key?.GetValue(RegistryValueName);
    if (registryValueObject == null)
    {
        return ApplicationTheme.Light;
    }
    var registryValue = (int)registryValueObject;

    return registryValue > 0 ? ApplicationTheme.Light : ApplicationTheme.Dark;
}

Full example can be found in this link https://engy.us/blog/2018/10/20/dark-theme-in-wpf/

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Gowshik
  • 320
  • 4
  • 13
3

Before the Windows Anniversary update you could not do that. The application theme was always the one you set in the App.xaml file:

<Application
  ...
  RequestedTheme="Dark">
</Application>

Now with the new Anniversary Update, you can remove this line from the App.xaml file, which will make the app honor the user's system settings.

The RequestedTheme enumeration has actually three values - Dark, Light and Default. Default is the value that reflects the system settings, Dark and Light force the theme.

If you want to actually detect the current theme in code when App's RequestedTheme is Default, you will probably need to check the some color resource like SystemAltHighColor for its value, because that will give you an idea of what theme is currently set.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
0

ThemeResources have been introduced in 8.1 and their behavior is similar in W10. Therefore you can define suitable resource in ThemeDictionaries responsible for available Themes and then you can check the defined resource when you whant to know which Theme is currently used.

The code will be very similar to the one in this answer.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
0

If you want to get the Value in PowerShell you can use the following code:

(New-Object Windows.UI.ViewManagement.UISettings).GetColorValue("background")
Tobias Meyer
  • 61
  • 1
  • 5