gpt4 book ai didi

.net - "Custom Event"在 VB.Net 中如何工作?

转载 作者:行者123 更新时间:2023-12-04 09:29:54 25 4
gpt4 key购买 nike

在 C# 中,如果我想创建一个“自定义事件”,您可以执行以下操作:

private EventHandler _MyEvent;
Public Event EventHandler MyEvent
{
add{ _MyEvent += value; }
remove{ _MyEvent -= Value; }
}
protected void RaiseMyEvent()
{
if(_MyEvent != nul)
_MyEvent(this, EventArgs.Empty);
}

在 VB 中,这不是那么简单,我也无法在网络上的任何地方找到任何帮助。
这不起作用:
private _MyEvent as EventHandler
Public Custom Event MyEvent as EventHandler
AddHandler(ByVal value as EventHandler)
_MyEvent += value 'does not work
addhandler _MyEvent, value 'does not work
[Delegate].Combine(_MyEvent, value) '_MyEvent still nothing
End AddHandler
RemoveHandler(ByVal value as EventHandler)
'Same kind of stuff here
End RemoveHandler
RaiseEvent(sender as Object, e as EventArgs)
if(_MyEvent IsNot Nothing) Then '_MyEvent is always nothing
_MyEvent.Invoke(sender, e)
End If
End RaiseEvent
End Event

请帮忙。

最佳答案

VB 为您完成了大量工作。

简单地声明一个事件:

Public Event DidSomething(sender as object, e as EventArgs)

然后在代码的任何地方,调用下降的 Raise 事件
RaiseEvent DidSomething(me, EventArgs.Empty)

关于.net - "Custom Event"在 VB.Net 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/389124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com