检查 RoutedEvent 是否有任何处理程序

发布于 2024-08-30 16:53:14 字数 1223 浏览 3 评论 0原文

我有一个自定义 Button 类,当单击它时(打开特定窗口),它总是执行相同的操作。我添加了一个可以在按钮的 XAML 中分配的 Click 事件,就像常规按钮一样。

当它被单击时,我想执行 Click 事件处理程序(如果已分配),否则我想执行默认操作。问题是显然没有办法检查是否有任何处理程序已添加到事件中。

我认为对事件进行空检查可以做到这一点:

if (Click == null) 
{ 
    DefaultClickAction(); 
} 
else 
{ 
    RaiseEvent(new RoutedEventArgs(ClickEvent, this));;
}

...但这不会编译。编译器告诉我,除了 += 或 -= 之外,我不能对定义类之外的事件执行任何操作,尽管我试图在定义类内部执行此检查。

我自己实现了正确的行为,但它丑陋且冗长,我不敢相信没有内置的方法来做到这一点。我一定是错过了什么。

这是相关代码:

public class MyButtonClass : Control
{
    //...

    public static readonly RoutedEvent ClickEvent =
        EventManager.RegisterRoutedEvent("Click",
                                         RoutingStrategy.Bubble,
                                         typeof(RoutedEventHandler),
                                         typeof(MyButtonClass));

    public event RoutedEventHandler Click
    {
        add { ClickHandlerCount++; AddHandler(ClickEvent, value); }
        remove { ClickHandlerCount--; RemoveHandler(ClickEvent, value); }
    }

    private int ClickHandlerCount = 0;

    private Boolean ClickHandlerExists
    {
        get { return ClickHandlerCount > 0; }
    }

    //...
}

I've got a custom Button class, that always performs the same action when it gets clicked (opening a specific window). I'm adding a Click event that can be assigned in the button's XAML, like a regular button.

When it gets clicked, I want to execute the Click event handler if one has been assigned, otherwise I want to execute the default action. The problem is that there's apparently no way to check if any handlers have been added to an event.

I thought a null check on the event would do it:

if (Click == null) 
{ 
    DefaultClickAction(); 
} 
else 
{ 
    RaiseEvent(new RoutedEventArgs(ClickEvent, this));;
}

...but that doesn't compile. The compiler tells me that I can't do anything other than += or -= to an event outside of the defining class, event though I'm trying to do this check INSIDE the defining class.

I've implemented the correct behavior myself, but it's ugly and verbose and I can't believe there isn't a built-in way to do this. I must be missing something.

Here's the relevant code:

public class MyButtonClass : Control
{
    //...

    public static readonly RoutedEvent ClickEvent =
        EventManager.RegisterRoutedEvent("Click",
                                         RoutingStrategy.Bubble,
                                         typeof(RoutedEventHandler),
                                         typeof(MyButtonClass));

    public event RoutedEventHandler Click
    {
        add { ClickHandlerCount++; AddHandler(ClickEvent, value); }
        remove { ClickHandlerCount--; RemoveHandler(ClickEvent, value); }
    }

    private int ClickHandlerCount = 0;

    private Boolean ClickHandlerExists
    {
        get { return ClickHandlerCount > 0; }
    }

    //...
}

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

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

发布评论

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

评论(1

谁人与我共长歌 2024-09-06 16:53:14

不,这是不可能的。实际上,您的代码假设它们正在处理您的控件本身上的事件,但您声明的是 Bubble 事件而不是 Direct 事件,因此从技术上讲,某些东西可以侦听该事件元素链的更上游。另外,从技术上讲,不需要使用 CLR 事件来挂钩事件;人们可以使用 AddHandler 方法直接传递路由事件,这就是有人必须做的才能将事件进一步挂接到链上。最后,如果有人为事件注册了类处理程序(每当为 MyButtonClass 的任何实例引发该事件时都会收到通知),则不会使用 CLR 事件。如果您查看类似 UIElement.BuildRouteHelper 的内容,您将看到 WPF 建立事件路由所经历的所有步骤以及引发事件时将调用的对象。

如果您确实需要知道是否有任何侦听器,那么您最好创建仅 CLR 事件而不是路由事件。然后您可以检查您的委托是否非空。

No it is not possible. Actually the code you have assumes that they are handling the event on your control itself but you are declaring a Bubble event not a Direct event so technically something can be listening to the event further up the element chain. Also, technically one doesn't need to use the CLR event to hook the event; one can just use the AddHandler method directly passing in your routed event and that is what someone would have to do to hook the event further up the chain. Lastly the CLR event won't be used if someone registers a class handler for the event (to be notified whenever that event is raised for any instance of your MyButtonClass). If you look at something like the UIElement.BuildRouteHelper you will see all the steps that WPF goes through to establish the event route and the objects that will be invoked when the event is raised.

If you really need to know if there are any listeners then you are better off creating a CLR only event and not a routed event. Then you can check if your delegate is non-null.

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