如何处理来自子窗口的路由事件

本文关键字:窗口 路由 事件 何处理 处理 | 更新日期: 2023-09-27 18:37:11

目的是从子窗口获取和处理路由事件。我不能(阅读:不想)使用直接路由,因为(命令)之间有更多的元素。

下面的示例演示事件路由从一个窗口到第二个窗口不起作用。子窗口 XAML:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
<Grid>
    <Button Content="Raise Routing Event" HorizontalAlignment="Left" Margin="50" VerticalAlignment="Top"
            Width="150" Click="RaiseRoutedEvent" />
</Grid>

引发事件代码:

using System.Windows;
namespace WpfApplication1
{
    public partial class Window1
    {
        private static readonly RoutedEvent ChildWindowEvent = EventManager.RegisterRoutedEvent("ButtonClicked",
            RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Window1));
        public Window1()
        {
            InitializeComponent();
        }
        public event RoutedEventHandler ButtonClicked
        {
            add { AddHandler(ChildWindowEvent, value); }
            remove { RemoveHandler(ChildWindowEvent, value); }
        }
        private void RaiseRoutedEvent(object sender, RoutedEventArgs e)
        {
            RoutedEventArgs eventArgs = new RoutedEventArgs(ChildWindowEvent);
            RaiseEvent(eventArgs);
        }
    }
}

主窗口:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication1="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525" wpfApplication1:Window1.ButtonClicked="HandleRoutedEvent">
    <Grid>
        <Button Content="Open new window" HorizontalAlignment="Left" Margin="50" VerticalAlignment="Top"
                Width="150" Click="OpenNewWindow" />
    </Grid>
</Window>

应处理路由事件的窗口:

using System.Windows;
namespace WpfApplication1
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void OpenNewWindow(object sender, RoutedEventArgs e)
        {
            Window1 window1 = new Window1();
            window1.ShowDialog();
        }
        private void HandleRoutedEvent(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("This message is shown from the Main Window");
        }
    }
}

该事件是从 Window1 引发的,但 MainWindow.HandleRoutedEvent 不会命中其断点。为什么?

如何处理来自子窗口的路由事件

路由

事件沿可视化树传输。 顶级窗口是可视化树根,不是其所有者的可视化树的一部分。 因此,从子窗口中冒出的任何事件都不会传播到所有者窗口。

顺便说一句,我注意到您的示例代码中存在几个问题。 在 xaml 中,使用附加的事件语法注册处理程序,但已声明实例事件。 如果要实现附加事件,则需要以下成员:

public static readonly RoutedEvent ButtonClickedEvent = EventManager.RegisterCrossWindowRoutedEvent(
    "ButtonClicked",
    RoutingStrategy.Bubble,
    typeof(RoutedEventHandler),
    typeof(ChildWindow));
public static void AddButtonClickedHandler(UIElement target, RoutedEventHandler handler)
{
    target.AddHandler(ButtonClickedEvent, handler);
}
public static void RemoveButtonClickedHandler(UIElement target, RoutedEventHandler handler)
{
    target.RemoveHandler(ButtonClickedEvent, handler);
}

如果您打算拥有实例事件,则事件名称应与注册路由事件时提供的名称相对应("ButtonClicked")。