在xaml代码中
>= 80
如何使用C#代码获取颜色?
答案 0 :(得分:1)
通过c#
更改颜色
Anything.Foreground = (SolidColorBrush)Application.Current.Resources["SystemControlForegroundAccentBrush"];
任何东西都是你的按钮,文字等等。任何支持它的东西
您可以使用Color.FromArgb()在代码中定义自定义颜色:
Anything.Foreground = new SolidColorBrush(Color.FromArgb(255, 225, 48, 221));
只需根据您的要求更改ARGB代码
如果您希望以后在应用中使用十六进制颜色代码,可以执行以下操作:
创建一个将十六进制字符串转换为SolidColorBrush的方法:
public SolidColorBrush GetSolidColorBrush(string hex)
{
hex = hex.Replace("#", string.Empty);
byte a = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16));
byte r = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16));
byte g = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16));
byte b = (byte)(Convert.ToUInt32(hex.Substring(6, 2), 16));
SolidColorBrush myBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
return myBrush;
}
现在剩下的就是通过调用方法获取颜色并将十六进制字符串作为参数传递给它:
var color = GetSolidColorBrush("#FFDC3569").Color;