WPF Routed Event,订阅自定义事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11692231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:45:26  来源:igfitidea点击:

WPF Routed Event, subscribing to custom events

wpfrouted-events

提问by landoncz

I am trying to get routed events working with child controls that will manually fire these events and they will bubble up and be handled at the main grid level. I basically want to do something like this:

我试图让路由事件与子控件一起使用,这些子控件将手动触发这些事件,它们将冒泡并在主网格级别进行处理。我基本上想做这样的事情:

<Grid Name="Root" WpfApplication5:SpecialEvent.Tap="Catcher_Tap">
    <Grid.RowDefinitions>
        <RowDefinition Height="40" />
        <RowDefinition Height="40" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <WpfApplication5:UserControl2 Grid.Row="0" x:Name="Catcher" />
    <WpfApplication5:UserControl1 Grid.Row="1" />
    <Frame Grid.Row="2" Source="Page1.xaml" />
</Grid>

But when I run my example, I get a null reference in the presentation framework, the application never initializes, it fails when it's trying to load/initialize the XAML (InitializeComponent()). Here's the small file that contains the event:

但是当我运行我的例子时,我在表示框架中得到一个空引用,应用程序永远不会初始化,它在尝试加载/初始化 XAML (InitializeComponent()) 时失败。这是包含事件的小文件:

public class SpecialEvent
{
    public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
        "Tap", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(UserControl1));

    // Provide CLR accessors for the event
    public event RoutedEventHandler Tap;
}

I am basically wanting to copy the behavior of how ButtonBase.Click allows parents to subscribe to any button click() methods for their children. But, this doesn't seem to be working for anything but ButtonBase.Click(). That is, when I switch out my custom WpfApplication5:SpecialEvent.Tap="Catcher_Tap"to ButtonBase.Click="Catcher_Tap"it works. Any ideas why? What is ButtonBase doing that I'm not doing?

我基本上想复制 ButtonBase.Click 如何允许父母为他们的孩子订阅任何按钮 click() 方法的行为。但是,这似乎对 ButtonBase.Click() 以外的任何东西都不起作用。也就是说,当我将我的习惯切换WpfApplication5:SpecialEvent.Tap="Catcher_Tap"ButtonBase.Click="Catcher_Tap"它时。任何想法为什么?ButtonBase 在做什么而我没有在做什么?

回答by landoncz

After playing around some more, I found that it's possible to accomplish what I needed in the code behind of the main window like so:

在玩了更多之后,我发现可以在主窗口后面的代码中完成我需要的东西,如下所示:

public MainWindow()
    {
        InitializeComponent();
        Root.AddHandler(SpecialEvent.TapEvent, new RoutedEventHandler(Catcher_Tap));
    }

For some reason, specifying it in the XAML as you would do for ButtonBase() does not work, but adding the Handlerin the code behind does.

出于某种原因,在 XAML 中指定它,就像您对 ButtonBase()所做的那样不起作用,但Handler在后面的代码中添加。

回答by Nathan Friend

The code you provided does register a custom event, however, it doesn't register an attachedcustom event. You'll have to explicitly implement the Add*Handlerand Remove*Handlermethods if you would like to use the attached syntax with your event. See the "Defining Your Own Attached Events as Routed Events" section on this MSDN article.

您提供的代码确实注册了自定义事件,但是,它没有注册附加的自定义事件。如果您想在事件中使用附加的语法,则必须显式实现Add*HandlerRemove*Handler方法。请参阅此 MSDN 文章中的“将您自己的附加事件定义为路由事件”部分。