在 xaml 代码中
Foreground="{ThemeResource SystemControlForegroundAccentBrush}"
如何在 C# 代码中获取颜色?
通过 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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句