Search code examples
windowsuwpwindows-10-universal

How can I detect Windows 10 light/dark mode?


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.


Solution

  • 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.