在C#中,您可以在add
订阅/取消订阅时定义明确的remove
/ event
代码。
这在VB.net中是否可行?
答案 0 :(得分:6)
Imports System.Runtime.CompilerServices
...
Private propchanged As PropertyChangedEventHandler
Public Custom Event PropertyChanged As PropertyChangedEventHandler
<MethodImpl(MethodImplOptions.Synchronized)> _
AddHandler(ByVal value As PropertyChangedEventHandler)
propchanged = DirectCast([Delegate].Combine(propchanged, value), PropertyChangedEventHandler)
End AddHandler
<MethodImpl(MethodImplOptions.Synchronized)> _
RemoveHandler(ByVal value As PropertyChangedEventHandler)
propchanged = DirectCast([Delegate].Remove(propchanged, value), PropertyChangedEventHandler)
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs)
Dim handler = propchanged
If handler IsNot Nothing Then handler(sender, e)
End RaiseEvent
End Event
答案 1 :(得分:1)
看起来可以使用定义here的自定义事件来完成。
' Define the MouseDown event property.
Public Custom Event MouseDown As MouseEventHandler
' Add the input delegate to the collection.
AddHandler(Value As MouseEventHandler)
listEventDelegates.AddHandler(mouseDownEventKey, Value)
End AddHandler
' Remove the input delegate from the collection.
RemoveHandler(Value As MouseEventHandler)
listEventDelegates.RemoveHandler(mouseDownEventKey, Value)
End RemoveHandler
' Raise the event with the delegate specified by mouseDownEventKey
RaiseEvent(sender As Object, e As MouseEventArgs)
Dim mouseEventDelegate As MouseEventHandler = _
listEventDelegates(mouseDownEventKey)
mouseEventDelegate(sender, e)
End RaiseEvent
End Event