如何检测 Windows 10 light/dark 模式?
How can I detect Windows 10 light/dark mode?
我正在使用 Windows.UI.ViewManagement.UISettings
获取系统强调色,但似乎此 class 没有任何方法或 属性 用于 light/dark 模式。我找不到此功能的文档,我该如何检测?
PS:我正在制作一个无法访问 Windows.UI.Xaml
命名空间的 JS 应用程序。
ThemeResources 已在 8.1 中引入,其行为在 W10 中类似。因此,您可以在 ThemeDictionaries 中定义合适的资源,负责可用的 Themes,然后您可以在想知道哪个 主题 当前使用。
该代码与 this answer 中的代码非常相似。
在 Windows 周年纪念更新之前,您无法做到这一点。应用程序主题始终是您在 App.xaml
文件中设置的主题:
<Application
...
RequestedTheme="Dark">
</Application>
现在有了新的周年更新,您可以从 App.xaml
文件中删除这一行,这将使应用程序遵循用户的系统设置。
RequestedTheme
枚举实际上有三个值 - Dark
、Light
和 Default
。 Default
是反映系统设置的值,Dark
和Light
强制主题。
如果你想在 App 的 RequestedTheme
为 Default
时实际检测代码中的当前主题,你可能需要检查一些颜色资源,如 SystemAltHighColor
的值,因为这会让您了解当前设置的主题。
您可以在您的解决方案中创建一个 Windows 运行时组件项目,从那里您可以访问 Windows.UI.Xaml
命名空间。添加一个方法来检查当前的 ApplicationTheme。
public sealed class Test
{
public static string CurrentTheme()
{
var isDark = Application.Current.RequestedTheme == ApplicationTheme.Dark;
if (isDark)
return "Dark";
return "Light";
}
}
在您的 javascript 应用程序项目中添加对 windows 运行时组件项目的引用,您可以在任何要检查应用程序主题的地方调用此方法。查看 here 创建 Windows 运行时组件的演练。
我找到了一个更简单的解决方案,它应该也适用于 JavaScript 应用程序,而不需要 Windows 运行时组件 - UISettings
class:
var uiSettings = new Windows.UI.ViewManagement.UISettings();
var color = uiSettings.getColorValue(
Windows.UI.ViewManagement.UIColorType.background
);
您获得的颜色是 black for dark theme 或 white for 浅色 主题。
class 也有非常有用的事件 ColorValuesChanged
,您可以使用它来 observe theme changes at runtime。
对于Windows10,注册表路径HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize
中AppsUseLightTheme
属性的值指定Windows是暗还是亮模式。
如果您想在 PowerShell 中获取值,可以使用以下代码:
(New-Object Windows.UI.ViewManagement.UISettings).GetColorValue("background")
如果为当前用户启用了主题,您可以读取注册表并从那里获取设置。像这样...
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;
}
完整的例子可以在这里找到link
https://engy.us/blog/2018/10/20/dark-theme-in-wpf/
我正在使用 Windows.UI.ViewManagement.UISettings
获取系统强调色,但似乎此 class 没有任何方法或 属性 用于 light/dark 模式。我找不到此功能的文档,我该如何检测?
PS:我正在制作一个无法访问 Windows.UI.Xaml
命名空间的 JS 应用程序。
ThemeResources 已在 8.1 中引入,其行为在 W10 中类似。因此,您可以在 ThemeDictionaries 中定义合适的资源,负责可用的 Themes,然后您可以在想知道哪个 主题 当前使用。
该代码与 this answer 中的代码非常相似。
在 Windows 周年纪念更新之前,您无法做到这一点。应用程序主题始终是您在 App.xaml
文件中设置的主题:
<Application
...
RequestedTheme="Dark">
</Application>
现在有了新的周年更新,您可以从 App.xaml
文件中删除这一行,这将使应用程序遵循用户的系统设置。
RequestedTheme
枚举实际上有三个值 - Dark
、Light
和 Default
。 Default
是反映系统设置的值,Dark
和Light
强制主题。
如果你想在 App 的 RequestedTheme
为 Default
时实际检测代码中的当前主题,你可能需要检查一些颜色资源,如 SystemAltHighColor
的值,因为这会让您了解当前设置的主题。
您可以在您的解决方案中创建一个 Windows 运行时组件项目,从那里您可以访问 Windows.UI.Xaml
命名空间。添加一个方法来检查当前的 ApplicationTheme。
public sealed class Test
{
public static string CurrentTheme()
{
var isDark = Application.Current.RequestedTheme == ApplicationTheme.Dark;
if (isDark)
return "Dark";
return "Light";
}
}
在您的 javascript 应用程序项目中添加对 windows 运行时组件项目的引用,您可以在任何要检查应用程序主题的地方调用此方法。查看 here 创建 Windows 运行时组件的演练。
我找到了一个更简单的解决方案,它应该也适用于 JavaScript 应用程序,而不需要 Windows 运行时组件 - UISettings
class:
var uiSettings = new Windows.UI.ViewManagement.UISettings();
var color = uiSettings.getColorValue(
Windows.UI.ViewManagement.UIColorType.background
);
您获得的颜色是 black for dark theme 或 white for 浅色 主题。
class 也有非常有用的事件 ColorValuesChanged
,您可以使用它来 observe theme changes at runtime。
对于Windows10,注册表路径HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize
中AppsUseLightTheme
属性的值指定Windows是暗还是亮模式。
如果您想在 PowerShell 中获取值,可以使用以下代码:
(New-Object Windows.UI.ViewManagement.UISettings).GetColorValue("background")
如果为当前用户启用了主题,您可以读取注册表并从那里获取设置。像这样...
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;
}
完整的例子可以在这里找到link https://engy.us/blog/2018/10/20/dark-theme-in-wpf/