0

我被困住了。我正在尝试AddHandler在 Page1 上设置一个按钮,这样当单击该按钮时,它将触发AddHandlerMainWindow_MouseUp事件,因此我可以单击 MainWindow 上的任何位置以显示一条消息。

我该如何解决这个问题?


笔记:

  • 我已经在 MainWindow_load 事件中删除了 MainWindow_MouseUp 的处理程序,因此在 MainWindow 加载时不会显示该消息。
  • 我有一个显示 Page1 的框架。
  • 当点击 Page1 中的按钮时,它应该是 AddHandler,所以 MainWindow_MouseUp 会在 MainWindow 执行 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>

第1页.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

主窗口.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>

主窗口.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
4

1 回答 1

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's方法。我怀疑这不是你想要做的。Button1_ClickMouseUp

现在这个:

RemoveHandler Me.MouseUp, AddressOf MainWindow_MouseUp

告诉我你不想MainWindow_MouseUp马上被勾搭上。Handles您只需删除以下子句即可摆脱这一行:

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

现在没有什么可以删除的了。

所以...我们回到通知 MainWindow Page1 的按钮已被单击的问题。为此,向 Page1 添加一个事件:

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

我在shared这里创建了这个事件——这允许我们在不创建 MainWindow 中的页面实例的情况下链接它(即:您可以简单地使用 Page1 作为源的框架)。我们在这里所做的是PageButtonClick在单击 Button1 时在 Page1 中引发一个事件。

在您的主窗口中,然后:

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

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

 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

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

Private Sub Window_Closed(sender As System.Object, e As System.EventArgs) _
                                                          Handles MyBase.Closed
    RemoveHandler Page1.PageButtonClick, AddressOf Page1_Clicked
    if _mouseupConnected then RemoveHandler Me.MouseUp, AddressOf Window_MouseUp
End Sub

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

于 2013-06-10T23:11:04.027 回答