WPF 中的类侦听器和实例侦听器有什么区别?

发布于 2024-10-16 01:30:19 字数 563 浏览 1 评论 0原文

我正在尝试了解一些 WPF 特定的内容,但尚未找到 UIElement.AddHandler 方法和 EventManager.RegisterClassHandler 方法之间的具体关系。

我用谷歌搜索了一下,发现了这篇有趣的 MSDN 文章:

http://msdn. microsoft.com/en-us/library/ms747183.aspx

这里指出:

“路由事件考虑两种不同类型的事件侦听器:类侦听器和实例侦听器。类侦听器存在是因为类型调用了特定的 EventManager API、RegisterClassHandler 在其静态构造函数中,或从元素基类重写了类处理程序虚拟方法。 实例侦听器是特定的类实例/元素,其中通过调用 AddHandler 为该路由事件附加了一个或多个处理程序。”

好吧,现在我知道了类和它的实例之间的区别,但不知怎的,我无法理解文档的这个特定部分。

谁能为我解决这个问题吗?

I am trying to wrap my head around some WPF specific stuff, and have yet to find the concrete relation between the UIElement.AddHandler method and the EventManager.RegisterClassHandler method.

I have googled a bit and found this interesting MSDN article:

http://msdn.microsoft.com/en-us/library/ms747183.aspx

Here it states:

"Routed events consider two different types of listeners to the event: class listeners and instance listeners. Class listeners exist because types have called a particular EventManager API, RegisterClassHandler, in their static constructor, or have overridden a class handler virtual method from an element base class. Instance listeners are particular class instances/elements where one or more handlers have been attached for that routed event by a call to AddHandler."

Alright now so I know the difference between a class and its instance, but somehow I cannot make sense out of this specific part of the document.

Can anyone clear that up for me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-10-23 01:30:19

我不知道,你到底想知道什么。事情非常简单:您可以在实例(对象)级别或类级别注册处理程序。

不同之处在于,当您在类级别注册事件时,它将在任何实例级别处理程序之前首先被调用(当然,如果处理类在逻辑树中较低/较高,则隧道或冒泡仍然会发生在之前)。因此,您可以在类级别处理此事件,并过滤此事件是否应由实例处理(通过设置 e.Handled = true ,您将停止事件通过其他处理程序)。在某些情况下它可能很有用,但目前我没有可以分享的例子。

此示例将注册事件处理程序,仅当为特定元素实例引发事件时才会调用该事件处理程序:

DockPanel panel = new DockPanel();
panel.AddHandler(Button.ClickEvent, new RoutedEventHandler(Button_Click));

这将创建事件处理程序,每次任何 DockPanel 获取 Button.Click 事件时都会调用该事件处理程序,在调用此 DockPanel 的实例处理程序之前:

EventManager.RegisterClassHandler(typeof(DockPanel),
    Button.ClickEvent, new RoutedEventHandler(ButtonClass_Click));

如果方法是:

private void ButtonClass_Click(object sender, RoutedEventArgs e)
{
    Debug.Write("Class level handler");
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    Debug.Write("Instance level handler");
}

这将创建输出:

类级别处理程序
实例级处理程序

但是如果在类级处理程序中将事件参数设置为handeled (e.Handled = true;),它会为实例级处理程序过滤掉该事件(并在逻辑树中冒泡) 。

I don't know, what exactly do you want to know. Things are pretty simple: you can register handler at instance (object) level, or at class level.

The difference is, when you register event at class level, it will get called first, before any instance level handlers (of course tunneling or bubbling still takes place before, if handling class is lower/higher in logical tree). So you can handle this event at class level and filter whether this event should be handled by instance or not (by setting e.Handled = true you will stop event for going through other handlers). It may be useful in some cases, but for now I have no example in my mind to share.

This sample will register event handler that will be called only when event was raised for specific instance of element:

DockPanel panel = new DockPanel();
panel.AddHandler(Button.ClickEvent, new RoutedEventHandler(Button_Click));

And this will create event handler, that will be called each time any DockPanel will get Button.Click event, before instance handler of this DockPanel will get called:

EventManager.RegisterClassHandler(typeof(DockPanel),
    Button.ClickEvent, new RoutedEventHandler(ButtonClass_Click));

If methods were:

private void ButtonClass_Click(object sender, RoutedEventArgs e)
{
    Debug.Write("Class level handler");
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    Debug.Write("Instance level handler");
}

This would create output:

Class level handler
Instance level handler

But if in class level handler you would set event args to handeled (e.Handled = true;), it would filter out this event for instance level handler (and bubbling up in logical tree).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文