mas*_*uko 4 c# xaml uwp uwp-xaml
我正在寻找一种方法来获取 Windows 10 根据背景图像自动选择的颜色,如下所示。
我尝试搜索,发现
var color = (Color)this.Resources["SystemAccentColor"];
Run Code Online (Sandbox Code Playgroud)
和
var color = (Color)Application.Current.Resources["SystemAccentColor"];
Run Code Online (Sandbox Code Playgroud)
但他们都是例外
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>
Run Code Online (Sandbox Code Playgroud)
在此代码中您将仅获得十六进制颜色:
Application.Current.Resources["SystemAccentColor"]
Run Code Online (Sandbox Code Playgroud)
您必须将其转换为可用的颜色格式,这是解决方案。
var color = Application.Current.Resources["SystemAccentColor"];
btnTest.Background = GetColorFromHex(color.ToString());
Run Code Online (Sandbox Code Playgroud)
这是转换函数:
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)
));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1365 次 |
| 最近记录: |