确定是否已附加事件

时间:2021-07-22 03:26:36

I have two objects - one that contains some code with will fire an event, and one that contains the handler for that event. I can't "AddHandler" in the Load of the first object, because an instance of the second object doesn't exist yet. When I raise my event, I want to check to see if a copy of object2 has been instantiated (easy to do), and if a handler has been attached to the event yet (not sure how to do this).

我有两个对象 - 一个包含一些代码将触发一个事件,另一个包含该事件的处理程序。我不能在第一个对象的Load中使用“AddHandler”,因为第二个对象的实例尚不存在。当我举起我的事件时,我想检查一下object2的副本是否已被实例化(易于操作),以及是否已将事件附加到事件(不知道如何执行此操作)。

I'm also open to another recommendation about how to do this instead. If I do my AddHandler in Object1.Load, and Object2 doesn't exist yet, then it will never handle my event, even if I create it later. Right now, in the code that fires the event, I've just resorted to doing a RemoveHandler and then an AddHandler every single time the event is raised, and then I know I'll attach when the object finally exists, but I know this is a crappy method.

我也愿意接受另一个关于如何做到这一点的建议。如果我在Object1.Load中执行我的AddHandler,并且Object2尚不存在,那么它将永远不会处理我的事件,即使我稍后创建它也是如此。现在,在触发事件的代码中,我只是在每次引发事件时都使用了一个RemoveHandler然后是一个AddHandler,然后我知道当对象最终存在时我会附加,但我知道这个是一种糟糕的方法。

I saw an article about something similar (Determine list of event handlers bound to event), and maybe I'm missing something in the translation, but I can't get the code to work on my custom event in VB.NET.

我看到一篇关于类似内容的文章(确定绑定到事件的事件处理程序列表),也许我在翻译中遗漏了一些内容,但是我无法让代码在VB.NET中处理我的自定义事件。

4 个解决方案

#1


You could also just have a bool field that you check before hooking the event.

您也可以在挂钩事件之前检查一个bool字段。

if not eventHooked then
 addhandler
 eventHooked = true
end if

Also if you need a good c# to vb converter http://www.tangiblesoftwaresolutions.com/ has one that can translate a 100 lines on the fly or less for or translate a project of a 1000 lines for free. More than that you have to purchase it, but that usually those limits will work just fine. No I am not trying to advertise for them :-)

此外,如果你需要一个好的c#转换器http://www.tangiblesoftwaresolutions.com/有一个可以动态翻译100行或更少的或免费翻译1000行的项目。不仅如此,你必须购买它,但通常这些限制将工作得很好。不,我不是想为他们做广告:-)

#2


VB.Net creates a special private member variable in the pattern of <YourEvent>Event that you can then use to test against Nothing.

VB.Net以 事件的模式创建一个特殊的私有成员变量,然后您可以使用它来测试Nothing。

Public Event MyClick As EventHandler

Private Sub OnMyClick()
    If MyClickEvent IsNot Nothing Then
        RaiseEvent MyClick(Me, New EventArgs())
    Else
        ' No event handler has been set.
        MsgBox("There is no event handler. That makes me sad.")
    End If
End Sub

http://blogs.msdn.com/b/vbteam/archive/2009/09/25/testing-events-for-nothing-null-doug-rothaus.aspx

#3


According to the responses here: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/9ec8ff1c-eb9b-4cb3-8960-9cd4b25434f2 (which seem to work according to my testing), a check for existing event handlers is done upon calling RaiseEvent. If you do not want to raise an event and just need to check to see if any handlers are attached, you can check the value of a hidden variable called <your_event_name>Event like:

根据这里的回复:http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/9ec8ff1c-eb9b-4cb3-8960-9cd4b25434f2(看起来根据我的测试工作),检查对于现有事件处理程序,在调用RaiseEvent时完成。如果您不想引发事件并且只需要检查是否附加了任何处理程序,则可以检查名为 事件的隐藏变量的值,如:

Public Event Foo As ActionFoo

If FooEvent IsNot Nothing Then...

#4


If you just want to know whether any handler has been attached, you should be able to check whether the event is null.

如果您只想知道是否附加了任何处理程序,您应该能够检查该事件是否为空。

if (MyButton.Click == null)
{
    MyButton.Click += myEventHandler;
}

(I'll let you translate that into VB)

(我会让你把它翻译成VB)

#1


You could also just have a bool field that you check before hooking the event.

您也可以在挂钩事件之前检查一个bool字段。

if not eventHooked then
 addhandler
 eventHooked = true
end if

Also if you need a good c# to vb converter http://www.tangiblesoftwaresolutions.com/ has one that can translate a 100 lines on the fly or less for or translate a project of a 1000 lines for free. More than that you have to purchase it, but that usually those limits will work just fine. No I am not trying to advertise for them :-)

此外,如果你需要一个好的c#转换器http://www.tangiblesoftwaresolutions.com/有一个可以动态翻译100行或更少的或免费翻译1000行的项目。不仅如此,你必须购买它,但通常这些限制将工作得很好。不,我不是想为他们做广告:-)

#2


VB.Net creates a special private member variable in the pattern of <YourEvent>Event that you can then use to test against Nothing.

VB.Net以 事件的模式创建一个特殊的私有成员变量,然后您可以使用它来测试Nothing。

Public Event MyClick As EventHandler

Private Sub OnMyClick()
    If MyClickEvent IsNot Nothing Then
        RaiseEvent MyClick(Me, New EventArgs())
    Else
        ' No event handler has been set.
        MsgBox("There is no event handler. That makes me sad.")
    End If
End Sub

http://blogs.msdn.com/b/vbteam/archive/2009/09/25/testing-events-for-nothing-null-doug-rothaus.aspx

#3


According to the responses here: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/9ec8ff1c-eb9b-4cb3-8960-9cd4b25434f2 (which seem to work according to my testing), a check for existing event handlers is done upon calling RaiseEvent. If you do not want to raise an event and just need to check to see if any handlers are attached, you can check the value of a hidden variable called <your_event_name>Event like:

根据这里的回复:http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/9ec8ff1c-eb9b-4cb3-8960-9cd4b25434f2(看起来根据我的测试工作),检查对于现有事件处理程序,在调用RaiseEvent时完成。如果您不想引发事件并且只需要检查是否附加了任何处理程序,则可以检查名为 事件的隐藏变量的值,如:

Public Event Foo As ActionFoo

If FooEvent IsNot Nothing Then...

#4


If you just want to know whether any handler has been attached, you should be able to check whether the event is null.

如果您只想知道是否附加了任何处理程序,您应该能够检查该事件是否为空。

if (MyButton.Click == null)
{
    MyButton.Click += myEventHandler;
}

(I'll let you translate that into VB)

(我会让你把它翻译成VB)