比较 Button.Background 与颜色
Comparing Button.Background with Colors
我正在构建一个 Windows 商店应用程序。我在尝试将背景与颜色进行比较时遇到问题。
我的程序做什么。屏幕上有许多按钮,单击任何按钮都会将其背景颜色更改为红色或绿色。从红色开始并按 click.Now 切换颜色 我希望已经被点击的按钮,它们的 background
不应该改变。从而后台检查if
语句跳过background
变色代码。
这是我的代码:
private void changecolor(object sender, RoutedEventArgs e)
{
if ((sender as Button).Background != "Red" && (sender as Button).Background != "Green")
{
if (counter == 1)
{
(sender as Button).Background = new SolidColorBrush(Windows.UI.Colors.Green);
(sender as Button).Content = "Green";
counter = 0;
}
else if (counter == 0)
{
(sender as Button).Background = new SolidColorBrush(Windows.UI.Colors.Red);
(sender as Button).Content = "Red";
counter = 1;
}
}
}
在第一个 if
语句中,我想检查 Background
是否不是 Red
或 Green
。
(sender as Button).Background != Windows.UI.Colors.Red
(sender as Button).Background != "Red"
以上代码无效。
我应该在 "Red" 的位置写什么来进行比较?
这是 Control
class(Button
继承自)的 MSDN 文档中的示例:
void ChangeBackground(object sender, RoutedEventArgs e)
{
if (btn.Background == Brushes.Red)
{
btn.Background = new LinearGradientBrush(Colors.LightBlue, Colors.SlateBlue, 90);
btn.Content = "Control background changes from red to a blue gradient.";
}
else
{
btn.Background = Brushes.Red;
btn.Content = "Background";
}
}
可查看全文here。
因此,要将此应用于您当前的代码,您只需将第一个 if
语句更改为如下所示:
var redBrush = new SolidColorBrush(Colors.Red);
var greenBrush = new SolidColorBrush(Colors.Green);
if ((sender as Button).Background == redBrush ||
(sender as Button).Background == greenBrush)
我终于找到了答案。
Thank you @dub stylee and @Hans Passant
我将 background
种姓化为 solidcolorbrush
然后使用其 color
属性 并将其与 Windows.Ui.Colors.Green
进行比较
这是代码。
if (((sender as Button).Background as SolidColorBrush).Color != Windows.UI.Colors.Green && ((sender as Button).Background as SolidColorBrush).Color != Windows.UI.Colors.Red)
我正在构建一个 Windows 商店应用程序。我在尝试将背景与颜色进行比较时遇到问题。
我的程序做什么。屏幕上有许多按钮,单击任何按钮都会将其背景颜色更改为红色或绿色。从红色开始并按 click.Now 切换颜色 我希望已经被点击的按钮,它们的 background
不应该改变。从而后台检查if
语句跳过background
变色代码。
这是我的代码:
private void changecolor(object sender, RoutedEventArgs e)
{
if ((sender as Button).Background != "Red" && (sender as Button).Background != "Green")
{
if (counter == 1)
{
(sender as Button).Background = new SolidColorBrush(Windows.UI.Colors.Green);
(sender as Button).Content = "Green";
counter = 0;
}
else if (counter == 0)
{
(sender as Button).Background = new SolidColorBrush(Windows.UI.Colors.Red);
(sender as Button).Content = "Red";
counter = 1;
}
}
}
在第一个 if
语句中,我想检查 Background
是否不是 Red
或 Green
。
(sender as Button).Background != Windows.UI.Colors.Red
(sender as Button).Background != "Red"
以上代码无效。
我应该在 "Red" 的位置写什么来进行比较?
这是 Control
class(Button
继承自)的 MSDN 文档中的示例:
void ChangeBackground(object sender, RoutedEventArgs e)
{
if (btn.Background == Brushes.Red)
{
btn.Background = new LinearGradientBrush(Colors.LightBlue, Colors.SlateBlue, 90);
btn.Content = "Control background changes from red to a blue gradient.";
}
else
{
btn.Background = Brushes.Red;
btn.Content = "Background";
}
}
可查看全文here。
因此,要将此应用于您当前的代码,您只需将第一个 if
语句更改为如下所示:
var redBrush = new SolidColorBrush(Colors.Red);
var greenBrush = new SolidColorBrush(Colors.Green);
if ((sender as Button).Background == redBrush ||
(sender as Button).Background == greenBrush)
我终于找到了答案。
Thank you @dub stylee and @Hans Passant
我将 background
种姓化为 solidcolorbrush
然后使用其 color
属性 并将其与 Windows.Ui.Colors.Green
这是代码。
if (((sender as Button).Background as SolidColorBrush).Color != Windows.UI.Colors.Green && ((sender as Button).Background as SolidColorBrush).Color != Windows.UI.Colors.Red)