【问题标题】:Comparing Button.Background with Colors比较 Button.Background 和颜色
【发布时间】:2015-06-12 17:39:16
【问题描述】:

我正在构建一个 Windows 应用商店应用程序。尝试将背景与颜色进行比较时遇到问题。


我的程序做什么。屏幕上有许多按钮,单击任何按钮都会将其背景颜色更改为红色或绿色。从红色开始,每次点击切换颜色。现在我希望已经点击的按钮,他们的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 是否不是RedGreen

(sender as Button).Background != Windows.UI.Colors.Red

(sender as Button).Background != "Red"

上面的代码不起作用。


我应该写什么来代替“红色”来进行比较?

【问题讨论】:

  • 也许您看到的错误代码可能会有所帮助。可以看到,将Background属性值设置为SolidColorBrush,这显然与带有颜色名称的string不一样。
  • @dubstylee 以下是错误运算符 '!=' 不能应用于类型为 'Windows.UI.Xaml.Media.Brush' 和 'Windows.UI.Color' 运算符 '!=' 的操作数不能应用于“Windows.UI.Xaml.Media.Brush”和“字符串”类型的操作数
  • 简短回答:将"Red" 替换为Brushes.Red。更详细的答案如下。
  • 它是一个画笔,有几种可能的具体画笔类型。取决于你如何设计它。也许您可以将 Background 转换为 (SolidColorBrush),这是一个常见的选择,现在您可以使用它的 Color 属性。也可以是 GradientBrush 或 TileBrush,它们当然不只有一种颜色。
  • @HansPassant 是的,投射背景效果很好。感谢您的帮助,我已经发布了答案。

标签: c# xaml winrt-xaml


【解决方案1】:

我终于得到了这个问题的答案。

谢谢@dub stylee 和@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)

【讨论】:

    【解决方案2】:

    这是来自 MSDN 文档的 Control 类(Button 继承自)的示例:

    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)
    

    【讨论】:

    • Brushes 使用什么标头,因为我似乎无法将它添加到我的代码中。当前上下文中不存在画笔
    • System.Windows.Media
    • 我无法添加该标题。这是我的标题..using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using System.Diagnostics;我有刷子而不是刷子..不像刷子那样工作
    • 我想如果Brushes.Red不起作用,你可以试试new SolidColorBrush(Colors.Red)
    • if ((sender as Button).Background != new SolidColorBrush(Colors.Red) && (sender as Button).Background != new SolidColorBrush(Colors.Green) ) 两个代码都没有给出错误,但它们仍然不起作用。
    猜你喜欢
    • 2019-11-10
    • 2011-02-23
    • 1970-01-01
    • 2013-09-23
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 2015-03-31
    相关资源
    最近更新 更多