我正在尝试为我的应用程序创建插件功能。如何将事件AddHandle到一个将事件转换为不同事件类型的函数。 例如,插件类包含Sub和Event,如下所示:
Structure ClickedEventArgs
property ClickedTime As DateTime
property Clicked As Boolean
End structure
Sub Clicked(sender As object, e As ClickedEventArgs)
RaiseEvent Clicked(Sender, e)
End sub
Sub PluginLoaded
AddHandle ClickObject.Clicked, AddressOf NewObject.Clicked
End Sub
NewObject是一个引用,只能处理具有以下结构的事件(e作为NewObjectEventArgs),因此,它们不能是AddHandled,因为签名不相同。
Structure NewObjectEventArgs
Property Clicked As Boolean
Property ClickedTime As DateTime
Property otherInfo As Object
End Structure
如何将ClickedEventArgs转换为NewObjectEventArgs和AddHandle呢?
我是否使用功能?如果是这样,我该怎么做?
答案 0 :(得分:0)
您需要处理这些事件并将其转发到NewObject
:
Sub PluginLoaded()
AddHandler ClickObject.Clicked,
Sub(sender, e)
NewObject.Clicked(
sender,
New NewObjectEventArgs() With {
.Clicked = e.Clicked,
.ClickedTime = e.ClickedTime,
.otherInfo = ??? ' leave null, or populate somehow
}
)
End Sub
End Sub
如果你需要在某个时候删除处理程序,你应该创建一个单独的子程序而不是使用lambda(或者你可以这样做,无论如何,你更喜欢):
Sub ClickObject_Clicked(sender As Object, e As ClickedEventArgs)
' Same code as above
End Sub