简体   繁体   中英

How can I take system app mode in Windows 10 (UWP, WPF)

I would like to use system theme settings in my desktop app. I need to use app mode (light or dark). When we use for example dark mode in Windows, and click "Change Theme"button in my app, theme mode in my app should change Theme mode to Windows app mode.

I find one solution, but that example shows how to get Theme name but not the App Mode in System Colors settings

public string GetTheme()
{
    string RegistryKey = @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes";
    string theme;         
    theme = (string)Registry.GetValue(RegistryKey, string.Empty, string.Empty);           
    theme = theme.Split('\\').Last().Split('.').First().ToString();

    return theme;
}

For uwp you could set app's them with the following method.

public static async Task SetRequestedThemeAsync(ElementTheme theme)
{
    foreach (var view in CoreApplication.Views)
    {
        await view.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            if (Window.Current.Content is FrameworkElement frameworkElement)
            {
                frameworkElement.RequestedTheme = theme;
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM