Windows Phone 8.1非Silverlight VB - 不改变矩形的颜色

时间:2015-09-03 22:00:20

标签: vb.net colors windows-phone-8.1 rectangles

我试图在用户按下4个按钮后让我的矩形改变颜色。但它不会。这是代码。

Private Sub button5_Click(sender As Object, e As RoutedEventArgs) Handles button5.Click

    If currentplayer.Text = "X" Then
        button5.Background = New SolidColorBrush(Windows.UI.Colors.Green)
        currentplayer.Text = "O"

    Else
        If currentplayer.Text = "O" Then
            button5.Background = New SolidColorBrush(Windows.UI.Colors.Blue)
            currentplayer.Text = "X"
       End If
    End If
    Win()
End Sub


Private Sub Win()

    Dim Green As New SolidColorBrush(Windows.UI.Colors.Green)

    If button2.Background Is Green And button5.Background Is Green And button8.Background Is Green And button4.Background Is Green Then
        square1Green.Fill = New SolidColorBrush(Windows.UI.Colors.Green)
    End If
End Sub

我只包含其中一个按钮,因为它们都是相同的,只是按钮编号不同。

UPDATE **

  Private Sub Win()

    ''SQUARE ONE
    Dim button2Background = TryCast(button2.Background, SolidColorBrush)
    Dim button5Background = TryCast(button5.Background, SolidColorBrush)
    Dim button8Background = TryCast(button8.Background, SolidColorBrush)
    Dim button4Background = TryCast(button4.Background, SolidColorBrush)

    If button2Background IsNot Nothing AndAlso button2Background.Color = Windows.UI.Colors.Green And button5Background IsNot Nothing AndAlso button5Background.Color = Windows.UI.Colors.Green And button8Background IsNot Nothing AndAlso button8Background.Color = Windows.UI.Colors.Green And button4Background IsNot Nothing AndAlso button4Background.Color = Windows.UI.Colors.Green Then
        square1Green.Fill = New SolidColorBrush(Windows.UI.Colors.Green)

    End If
End Sub

更新2。 对不起,问题是如何使用此方法制作其他if语句? 我喜欢的是,如果有4个按钮颜色和currentplayer.text =" X"那么正方形将是绿色,但是如果按钮都有颜色,则currentplayer.text =" O"然后广场将填充蓝色。

再次感谢您的帮助:)

Private Sub Win()

    ''SQUARE ONE
    Dim button2Background = TryCast(button2.Background, SolidColorBrush)
    Dim button5Background = TryCast(button5.Background, SolidColorBrush)
    Dim button8Background = TryCast(button8.Background, SolidColorBrush)
    Dim button4Background = TryCast(button4.Background, SolidColorBrush)

    If currentplayer.Text = "O" And button2Background IsNot Nothing AndAlso button2Background.Color = Windows.UI.Colors.Green Or button2Background.Color = Windows.UI.Colors.Blue And button5Background IsNot Nothing AndAlso button5Background.Color = Windows.UI.Colors.Green Or button5Background.Color = Windows.UI.Colors.Blue And button8Background IsNot Nothing AndAlso button8Background.Color = Windows.UI.Colors.Green Or button8Background.Color = Windows.UI.Colors.Blue And button4Background IsNot Nothing AndAlso button4Background.Color = Windows.UI.Colors.Green Or button4Background.Color = Windows.UI.Colors.Blue Then
        square1Green.Fill = New SolidColorBrush(Windows.UI.Colors.Green)

    Else
        If currentplayer.Text = "X" And button2Background IsNot Nothing AndAlso button2Background.Color = Windows.UI.Colors.Green Or button2Background.Color = Windows.UI.Colors.Blue And button5Background IsNot Nothing AndAlso button5Background.Color = Windows.UI.Colors.Green Or button5Background.Color = Windows.UI.Colors.Blue And button8Background IsNot Nothing AndAlso button8Background.Color = Windows.UI.Colors.Green Or button8Background.Color = Windows.UI.Colors.Blue And button4Background IsNot Nothing AndAlso button4Background.Color = Windows.UI.Colors.Green Or button4Background.Color = Windows.UI.Colors.Blue Then
            square1Green.Fill = New SolidColorBrush(Windows.UI.Colors.Blue)

        End If

    End If
End Sub

1 个答案:

答案 0 :(得分:0)

Is是参考运算符;它检查两个引用最终是指同一个对象。由于Green被声明为new,因此button2.Background Is Green永远不会成立,因为button2.Background是对不同 SolidColorBrush的引用。它们都是绿色的事实对Is运算符毫无意义。

  

Is运算符确定两个对象引用是否引用相同   宾语。 但是,它不执行值比较。如果object1和   object2都指向完全相同的对象实例,结果为True;   如果他们不这样做,结果就是假的。   https://msdn.microsoft.com/en-us/library/kb136x1y.aspx

你可能需要做一些事情(对不起,暂时还没有完成VB)

Dim button2Background = TryCast(button2.Background, SolidColorBrush)
If button2Background isNot Nothing AndAlso button2Background.Color = Windows.UI.Colors.Green...