如何将不同的EventArgs传递给另一个子

时间:2019-08-13 14:26:28

标签: vb.net

我是VB.NET的新手,我正在尝试尝试从另一个调用Sub。我有第一个Sub处理一个按钮,然后执行一些代码。然后,我想调用另一个Sub来处理正被点击的矩形。我无法从第一个Sub呼叫第二个Sub。我认为可能是EventArgs。第一个SubRoutedEventsArgs,第二个TappedRoutedEventArgs。我似乎找不到有关此特定实例的任何文档。我只看到它是否与e相同。

Public Class MainPage

    Private Sub Show_Message(sender As Object, e As RoutedEventArgs) Handles Button_1.Click
        If Message.Visibility = Visibility.Collapsed Then
            Message.Visibility = Visibility.Visible
        Else
            Message.Visibility = Visibility.Collapsed
        End If

        ChangeColorItem_Click(sender, e)
    End Sub

    Private Sub ChangeColorItem_Click(sender As Object, e As TappedRoutedEventArgs) Handles Rectangle.Tapped

        'Change fill color to red to blue and back 
        If rectangleFill.Color = (Windows.UI.Colors.Red) Then
            rectangleFill.Color = Windows.UI.Colors.Blue
        Else
            rectangleFill.Color = Windows.UI.Colors.Red
        End If
    End Sub
End Class

我想要它,以便如果运行Show_Message时也运行ChangeColorItem_Click

1 个答案:

答案 0 :(得分:0)

RoutedEventArgs是一个方形钉,TappedRoutedEventArgs是一个圆孔...您正试图将一个方形钉放在一个圆孔中……而您不需要。

除非需要使用事件args,而且我看不到使用了事件args,否则最好将代码放到自己的子代码中并简单地调用它。

Public Class MainPage

    Private Sub Show_Message(sender As Object, e As RoutedEventArgs) Handles Button_1.Click
        ShowMessage()
    End Sub

    Private Sub ChangeColorItem_Click(sender As Object, e As TappedRoutedEventArgs) Handles Rectangle.Tapped

        ChangeColorItem()
    End Sub

    Private Sub ShowMessage()
        If Message.Visibility = Visibility.Collapsed Then
            Message.Visibility = Visibility.Visible
        Else
            Message.Visibility = Visibility.Collapsed
        End If

        ChangeColorItem()
    End Sub

    Private Sub ChangeColorItem()
        'Change fill color to red to blue and back 
        If rectangleFill.Color = (Windows.UI.Colors.Red) Then
            rectangleFill.Color = Windows.UI.Colors.Blue
        Else
            rectangleFill.Color = Windows.UI.Colors.Red
        End If
    End Sub
End Class