[英]UWP/WinUI3 How to get system theme in C#?
Is there a programming way to access the system theme (ie, theme for Windows)?是否有编程方式来访问系统主题(即 Windows 主题)?
The similar question #UWP get system theme (Light/Dark)
is answered here :类似的问题#UWP get system theme (Light/Dark)
在这里得到回答:
var DefaultTheme = new Windows.UI.ViewManagement.UISettings();
var uiTheme = DefaultTheme.GetColorValue(Windows.UI.ViewManagement.UIColorType.Background).ToString();
But as tipa
comments, the accepted answer suggests a way to access the theme for applications, not the theme for Windows.但正如tipa
评论的那样,接受的答案提出了一种访问应用程序主题的方法,而不是 Windows 的主题。
Therefore, I wonder if there are other ways to access the system theme.因此,我想知道是否有其他方法可以访问系统主题。
Here is a method I have used previously in WPF applications to determine if Windows is in High Contrast or Dark theme.这是我之前在 WPF 应用程序中使用的一种方法,用于确定 Windows 是高对比度主题还是深色主题。
It hasn't been updated for a while so it maybe out of date, but might be a starting point?它已经有一段时间没有更新了,所以它可能已经过时了,但可能是一个起点? You can easily adapt it to return an enum or bool for just light/dark if required.如果需要,您可以轻松地调整它以返回一个枚举或布尔值来表示亮/暗。
private static string GetWindowsTheme()
{
string RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
string RegistryValueName = "AppsUseLightTheme";
if (SystemParameters.HighContrast)
return "High Contrast";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyPath))
{
object registryValueObject = key?.GetValue(RegistryValueName);
if (registryValueObject == null)
return "Default";
int registryValue = (int)registryValueObject;
return registryValue > 0 ? "Default" : "Dark Theme";
}
}
Try this:尝试这个:
[DllImport("UXTheme.dll", SetLastError = true, EntryPoint = "#138")]
public static extern bool ShouldSystemUseDarkMode();
If the system uses dark mode, it will return true.如果系统使用暗模式,它将返回 true。
That's not the theme for applications.那不是应用程序的主题。
In WinUI 3, the enum ApplicationTheme
is defined in the Microsoft.UI.Xaml namespace.在 WinUI 3 中,枚举ApplicationTheme
在Microsoft.UI.Xaml命名空间中定义。
public enum ApplicationTheme
{
//
// Summary:
// Use the **Light** default theme.
Light,
//
// Summary:
// Use the **Dark** default theme.
Dark
}
You can get the theme like this.你可以得到这样的主题。
public App()
{
this.InitializeComponent();
ApplicationTheme theme = RequestedTheme;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.