使用WPF和VB.NET从Page1的AddHandler到Show Message MainWIndow

时间:2013-06-10 15:55:06

标签: wpf vb.net events mouseup addhandler

我被困住了。我正在尝试AddHandler找到Page1上的按钮,这样当点击按钮时,它会AddHandlerMainWindow_MouseUp事件,因此我可以点击MainWindow上的任意位置来显示消息。

如何解决此问题?


注意:

  • 我在MaindWindow_load事件中删除了MainWindow_MouseUp的Handler,因此在MainWindow加载时不显示该消息。
  • 我有一个显示Page1。
  • 的框架
  • 当单击Page1中的按钮时,它应该是AddHandler,因此当MainWindow执行MainWindow_MouseUp事件时,MainWindow_MouseUp将显示一条消息。

的Page1.xaml

<Page x:Class="Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="300"
    Title="Page1" Background="Red">
    <Grid>
        <Button Content="AddHandler for MainWindow MouseUp message"
                Height="42"
                HorizontalAlignment="Left"
                Margin="0,126,0,0"
                Name="Button1"
                VerticalAlignment="Top"
                Width="300" />
        <TextBlock Height="84"
                   HorizontalAlignment="Left"
                   Margin="80,184,0,0"
                   Name="TextBlock1"
                   Text="The button will add a handler in order to show a message on the MainWindow MouseUp event"
                   VerticalAlignment="Top"
                   Width="151"
                   TextWrapping="Wrap" />
        <TextBlock Height="23"
                   HorizontalAlignment="Left"
                   Margin="12,56,0,0"
                   Name="TextBlock2"
                   Text="Page1"
                   VerticalAlignment="Top" />
    </Grid>
</Page>

Page1.vb

Class Page1

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.Windows.RoutedEventArgs) _
                              Handles Button1.Click

        Dim _MainWindow As New MainWindow
        AddHandler _MainWindow.MouseUp, AddressOf Button1_Click

    End Sub

End Class

MainWindow.xaml

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Frame Source="Page1.xaml"
               Height="300"
               HorizontalAlignment="Left"
               Margin="110,-1,0,0"
               Name="Frame1"
               VerticalAlignment="Top"
               Width="300" />

        <TextBlock Height="144"
                   HorizontalAlignment="Left"
                   Margin="0,48,0,0"
                   Name="TextBlock2"
                   Text="Click here to show a message if handler MainWindow MouseUp is added from page1"
                   VerticalAlignment="Top"
                   TextWrapping="Wrap"
                   Width="104"
                   Foreground="#FF003AFF" />
        <TextBlock Height="153"
                   HorizontalAlignment="Left"
                   Margin="416,159,0,0"
                   Name="TextBlock3"
                   Text="Click here to show a message if handler MainWindow MouseUp is added from page1"
                   VerticalAlignment="Top"
                   TextWrapping="Wrap"
                   Foreground="#FFEF00FF" />
    </Grid>
</Window>

MainWindow.vb

Public Class MainWindow

    Private Sub Window_Loaded(ByVal sender As System.Object, _
                              ByVal e As System.Windows.RoutedEventArgs) _
                              Handles MyBase.Loaded

        RemoveHandler Me.MouseUp, AddressOf MainWindow_MouseUp

    End Sub
    Private Sub MainWindow_MouseUp(ByVal sender As Object, _
                                   ByVal e As MouseEventArgs) _
                                   Handles Me.MouseUp
        Test1()

    End Sub

    Public Sub Test1()
        MessageBox.Show("AddHandler added from page 1 was successful!")
    End Sub

End Class

1 个答案:

答案 0 :(得分:1)

这只是有点困惑。让我们来看看你在做什么:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
                           System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim _MainWindow As New MainWindow
    AddHandler _MainWindow.MouseUp, AddressOf Button1_Click
End Sub

这是一个新的MainWindow并将其MouseUp事件链接到方法Button1_Click - 您告诉_MainWindow执行Page1的{​​{1}}方法触发其Button1_Click事件。我怀疑这不是你想要做的。

现在这个:

MouseUp

告诉我你不希望RemoveHandler Me.MouseUp, AddressOf MainWindow_MouseUp 立刻被连接起来。只需删除MainWindow_MouseUp子句就可以摆脱这一行:

Handles

现在没有什么可以删除。

所以...我们回到了通知MainWindow点击了Page1的按钮的问题。为此,请向Page1:

添加一个事件
Private Sub MainWindow_MouseUp(ByVal sender As Object, _
                                ByVal e As MouseEventArgs) 'Handles Me.MouseUp
   Test1()
End Sub

我在这里制作了事件Class Page1 Shared Event PageButtonClick(sender As Object, e As RoutedEventArgs) Private Sub Button1_Click(sender As System.Object, e As _ System.Windows.RoutedEventArgs) Handles Button1.Click RaiseEvent PageButtonClick(sender, e) End Sub End Class - 这允许我们在没有在MainWindow中创建页面实例的情况下将其链接起来(即:您可以使用Page1作为源只有一个框架)。我们在这里做的是在点击Button1时在Page1中引发shared事件。

在主窗口中,然后:

PageButtonClick

现在,上述内容非常危险,因为只要单击该按钮,您就会始终添加处理程序。你应该总是非常小心地添加处理程序 - 每当你添加一个处理程序时,你必须确保在释放任何连接它之前删除它(否则最终会导致内存泄漏,因为处理程序可以根对象!)。我总是这样做(在MainWindow中):

Class MainWindow

    Private Sub Window_Loaded(sender As System.Object, e As _
                          System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        'Here we listen for the Page1Click event and execute the method
        'Page1_Clicked when we hear it
        AddHandler Page1.PageButtonClick, AddressOf Page1_Clicked
    End Sub

    Private Sub Window_MouseUp(sender As System.Object, e As _
                                     System.Windows.Input.MouseButtonEventArgs)
        MessageBox.Show("Hello")
    End Sub

    Private Sub Page1_Clicked(sender As Object, e As RoutedEventArgs)
        'When we get a click from Page1, add a handler to this window's 
        'MouseUp event and have it execute the Sub Window_Mouseup
        AddHandler Me.MouseUp, AddressOf Window_MouseUp
    End Sub

End Class

这确保您只添加一次处理程序。现在要摆脱它:

 Private _mouseUpConnected As Boolean = False

 Private Sub Page1_Clicked()
    If Not _mouseUpConnected Then
        AddHandler Me.MouseUp, AddressOf Window_MouseUp
        _mouseUpConnected = True
    End If
End Sub

这将处理PageButtonClick处理程序 - 它在窗口加载时添加,并在关闭时删除。它还负责处理MouseUp处理程序 - 如果已经连接,我们会在关闭之前将其断开连接。这可确保MainWindow正确地进行垃圾回收。