C# 以编程方式设置按钮的前景文本颜色
这是一个UWP应用程序。ReturnCharacter()将返回o或x。如果返回x,则设置蓝色。Else(返回o)并设置红色。然后,使用先前设置的颜色在按钮上写入x或o。因此,我们应该在按钮上显示蓝色的x或红色的o。尝试了以下操作,但无效。我只得到一个棕色的x或o。不知道那颜色是从哪里来的。还有,有没有一种方法可以将十六进制值用于颜色而不是RGB值C# 以编程方式设置按钮的前景文本颜色,c#,uwp,C#,Uwp,这是一个UWP应用程序。ReturnCharacter()将返回o或x。如果返回x,则设置蓝色。Else(返回o)并设置红色。然后,使用先前设置的颜色在按钮上写入x或o。因此,我们应该在按钮上显示蓝色的x或红色的o。尝试了以下操作,但无效。我只得到一个棕色的x或o。不知道那颜色是从哪里来的。还有,有没有一种方法可以将十六进制值用于颜色而不是RGB值 private void Button1_Click(object sender, RoutedEventArgs e) {
private void Button1_Click(object sender, RoutedEventArgs e)
{
char c = ReturnCharacter();
if (c == 'x')
{
button1.Foreground = new SolidColorBrush(Color.FromArgb(51, 178, 255, 0));
}
else
{
button1.Foreground = new SolidColorBrush(Color.FromArgb(255, 104, 51, 0));
}
button1.Content = c;
}
Color.FromArgb()
接受4个参数。第一个是阿尔法通道。如果希望颜色完全不透明,255是正确的值
您可能想要:
private void Button1_Click(object sender, RoutedEventArgs e)
{
char c = ReturnCharacter();
if (c == 'x')
{
button1.Foreground = new SolidColorBrush(Color.FromArgb(255, 51, 178, 255));
}
else
{
button1.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 104, 51));
}
button1.Content = c;
}
或者更简单有效地:
SolidColorBrush blue = new SolidColorBrush(Color.FromArgb(255, 51, 178, 255));
SolidColorBrush red = new SolidColorBrush(Color.FromArgb(255, 255, 104, 51))
private void Button1_Click(object sender, RoutedEventArgs e)
{
char c = ReturnCharacter();
button1.Foreground = c == 'x' ? blue : red;
button1.Content = c;
}
Color.FromArgb()
接受4个参数。第一个是阿尔法通道。如果希望颜色完全不透明,255是正确的值
您可能想要:
private void Button1_Click(object sender, RoutedEventArgs e)
{
char c = ReturnCharacter();
if (c == 'x')
{
button1.Foreground = new SolidColorBrush(Color.FromArgb(255, 51, 178, 255));
}
else
{
button1.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 104, 51));
}
button1.Content = c;
}
或者更简单有效地:
SolidColorBrush blue = new SolidColorBrush(Color.FromArgb(255, 51, 178, 255));
SolidColorBrush red = new SolidColorBrush(Color.FromArgb(255, 255, 104, 51))
private void Button1_Click(object sender, RoutedEventArgs e)
{
char c = ReturnCharacter();
button1.Foreground = c == 'x' ? blue : red;
button1.Content = c;
}
Color.FromArgb
期望A、R、G、B组件,顺序如下
难怪您会得到以下颜色:
您希望这样做:
Color.FromArgb
期望A、R、G、B组件,顺序如下
难怪您会得到以下颜色:
您希望这样做:
对于第一个解决方案,我总是得到红色/橙色的x或o。从来都不是蓝色的。第二种解决方案是无法将类型“Windows.UI.Xaml.Media.SolidColorBrush”隐式转换为“Windows.UI.Color”,第一个解决方案正在运行。我在检查是否有错误的字符。第二个解决方案仍然存在隐式转换问题。修复了第二个问题。感谢您的回答。对于第一个解决方案,我总是得到红色/橙色的x或o。从来都不是蓝色的。对于第二个解决方案,它说“无法隐式地将类型“Windows.UI.Xaml.Media.SolidColorBrush”转换为“Windows.UI.Color”。第一个解决方案正在工作。我检查了错误的字符。第二个解决方案仍然存在隐式转换问题。修复了第二个问题。感谢您提供了很好的答案。