如何获得 Windows 10 种强调色?
How to get Windows 10 accent color?
我正在寻找一种方法来获得 windows 10 根据背景图像自动选择的颜色,如下所示。
我尝试搜索,找到了
var color = (Color)this.Resources["SystemAccentColor"];
和
var color = (Color)Application.Current.Resources["SystemAccentColor"];
但两者都是例外
System.Exception
HResult=0x8000FFFF
Message=Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
您将在此代码中仅获得十六进制颜色:
Application.Current.Resources["SystemAccentColor"]
您必须将其转换为可用的颜色格式,这是解决方案。
var color = Application.Current.Resources["SystemAccentColor"];
btnTest.Background = GetColorFromHex(color.ToString());
这里是转换函数:
public static SolidColorBrush GetColorFromHex(string hexaColor)
{
return new SolidColorBrush(
Color.FromArgb(
Convert.ToByte(hexaColor.Substring(1, 2), 16),
Convert.ToByte(hexaColor.Substring(3, 2), 16),
Convert.ToByte(hexaColor.Substring(5, 2), 16),
Convert.ToByte(hexaColor.Substring(7, 2), 16)
));
}
我正在寻找一种方法来获得 windows 10 根据背景图像自动选择的颜色,如下所示。
我尝试搜索,找到了
var color = (Color)this.Resources["SystemAccentColor"];
和
var color = (Color)Application.Current.Resources["SystemAccentColor"];
但两者都是例外
System.Exception
HResult=0x8000FFFF
Message=Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
您将在此代码中仅获得十六进制颜色:
Application.Current.Resources["SystemAccentColor"]
您必须将其转换为可用的颜色格式,这是解决方案。
var color = Application.Current.Resources["SystemAccentColor"];
btnTest.Background = GetColorFromHex(color.ToString());
这里是转换函数:
public static SolidColorBrush GetColorFromHex(string hexaColor)
{
return new SolidColorBrush(
Color.FromArgb(
Convert.ToByte(hexaColor.Substring(1, 2), 16),
Convert.ToByte(hexaColor.Substring(3, 2), 16),
Convert.ToByte(hexaColor.Substring(5, 2), 16),
Convert.ToByte(hexaColor.Substring(7, 2), 16)
));
}