我正在寻找一种获取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>
答案 0 :(得分:3)
在此代码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)
)
);
}
希望这会有所帮助,