【问题标题】:How to check for the current background color of a grid?如何检查网格的当前背景颜色?
【发布时间】:2011-07-01 11:33:02
【问题描述】:

我基本上是在尝试制作一个切换按钮来更改 Windows Phone 7 应用程序的背景颜色。

我正在使用以下代码更改名为 LayoutRoot 的网格的背景颜色:

LayoutRoot.Background = new SolidColorBrush(Colors.White);

完成此操作后,我想在 if 语句中检查 LayoutRoot.Background 的值(用作切换)。这是我遇到问题的地方。我似乎想不出一种方法来检查该值。

当我执行LayoutRoot.Background.ToString() 时,我得到System.Windows.Media.SolidBrushColor 作为值。我想这是有道理的,因为背景是 SolidBrushColor。但是如何访问该值,以便在 if 语句中检查它?

【问题讨论】:

    标签: c# windows-phone-7


    【解决方案1】:

    你可以这样做:

    SolidColorBrush brush = LayoutRoot.Background as SolidColorBrush;
    if (brush != null) {
        if (brush.Color == Colors.White) {
            // Do something
        }
    }
    

    其他可能的画笔包括 LinearGradientBrush 和 RadialGradientBrush,因此 SolidColorBrush 只是众多可能的画笔类型之一。这就是为什么有一个 if 语句检查 null 的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-21
      • 2012-06-12
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 2023-02-22
      相关资源
      最近更新 更多