gpt4 book ai didi

VB.NET: "AddHandler"通过将 Handler 函数指定为变量

转载 作者:行者123 更新时间:2023-12-04 12:43:03 27 4
gpt4 key购买 nike

我正在为 Autodesk Inventor(一种 3D CAD 程序)编写插件。该插件向 Inventor 添加了一堆内部命令,并为每个命令创建了一个工具栏按钮。我需要将每个命令的执行事件连接到我的加载项中的处理函数。

我创建了一个“MyCommand”类,其中包含定义命令所需的所有信息。我还创建了一个 List(Of MyCommand),其中包含每个命令的定义。最后,我可以遍历每个命令并创建内部 Inventor 命令定义,以及将按钮添加到工具栏。但是,我不知道如何在循环内 将命令与其处理程序函数相关联。

下面的示例代码说明了我所追求的:

Sub CreateCommands()
Dim oCommands As New List(Of MyCommand)

oCommands.Add(New MyCommand("DrawLogoCmd", "Draw Logo", "DrawLogoSub"))
oCommands.Add(New MyCommand("PublishDrawingCmd", "Publish Drawing", "PublishDrawingSub"))
' [Dozens more commands]

For Each oCommand As MyCommand In oCommands
' Code for adding internal command definition and button to Inventor. This is working fine.
Dim oCommandDef As Inventor.CommandDefinition = InventorApp.CommandDefinitions.Add(oCommand.InternalDefinitionName, oCommand.DisplayName)
InventorApp.ToolbarButtons.Add(oCommandDef)

' Associate command definition with handler function
' ===== THIS IS THE LINE I NEED TO FIGURE OUT =====
AddHandler oCommandDef.OnExecute, AddressOf oCommand.HandlerSubName
Next
End Sub

Sub DrawLogoSub()
' [My add-in's code to draw logo in Inventor]
End Sub

Sub PublishDrawingSub()
' [My add-in's code to publish drawing in Inventor]
End Sub

Class MyCommand
Public InternalDefinitionName As String
Public DisplayName As String
Public HandlerSubName As String

Sub New(InternalDefinitionName As String, DisplayName As String, HandlerSubName As String)
With Me
.InternalDefinitionName = InternalDefinitionName
.DisplayName = DisplayName
.HandlerSubName = HandlerSubName
End With
End Sub
End Class

那么,这可能吗?有什么方法可以使用它的名称作为字符串来获取“AddressOf”我的函数吗?或者,是否有某种方法可以在我的 MyCommand 类中存储对函数本身的引用,并将其传递给 AddressOf 运算符?

或者,除了“AddHandler/AddressOf”之外,还有其他可行的方法吗?

如有任何建议,我们将不胜感激。

最佳答案

是的,您可以使用反射按名称找到该方法,但我不推荐这样做。 AddHandler 命令通常被赋予 AddressOf 方法,但它也可以采用委托(delegate),只要它符合事件的签名。因此,除非您需要通过字符串来完成,否则我建议您改为这样做。假设事件匹配普通的 EventHandler 委托(delegate):

Sub CreateCommands()
Dim oCommands As New List(Of MyCommand)

oCommands.Add(New MyCommand("DrawLogoCmd", "Draw Logo", AddressOf DrawLogo))
oCommands.Add(New MyCommand("PublishDrawingCmd", "Publish Drawing", AddressOf PublishDrawing))
' [Dozens more commands]

For Each oCommand As MyCommand In oCommands
' Code for adding internal command definition and button to Inventor. This is working fine.
Dim oCommandDef As Inventor.CommandDefinition = InventorApp.CommandDefinitions.Add(oCommand.InternalDefinitionName, oCommand.DisplayName)
InventorApp.ToolbarButtons.Add(oCommandDef)

' Associate command definition with handler function
AddHandler oCommandDef.OnExecute, oCommand.Handler
Next
End Sub

Sub DrawLogo(sender As Object, e As EventArgs)
' [My add-in's code to draw logo in Inventor]
End Sub

Sub PublishDrawing(sender As Object, e As EventArgs)
' [My add-in's code to publish drawing in Inventor]
End Sub

Class MyCommand
Public InternalDefinitionName As String
Public DisplayName As String
Public Handler As EventHandler

Sub New(InternalDefinitionName As String, DisplayName As String, Handler As EventHandler)
With Me
.InternalDefinitionName = InternalDefinitionName
.DisplayName = DisplayName
.Handler = Handler
End With
End Sub
End Class

如果你真的需要通过字符串来完成,你可以像这样使用反射(注意我使用了 NameOf 而不是将方法名称硬编码为字符串文字,只是为了更安全) :

Sub CreateCommands()
Dim oCommands As New List(Of MyCommand)

oCommands.Add(New MyCommand("DrawLogoCmd", "Draw Logo", NameOf(DrawLogo)))
oCommands.Add(New MyCommand("PublishDrawingCmd", "Publish Drawing", NameOf(PublishDrawing)))
' [Dozens more commands]

For Each oCommand As MyCommand In oCommands
' Code for adding internal command definition and button to Inventor. This is working fine.
Dim oCommandDef As Inventor.CommandDefinition = InventorApp.CommandDefinitions.Add(oCommand.InternalDefinitionName, oCommand.DisplayName)
InventorApp.ToolbarButtons.Add(oCommandDef)

' Associate command definition with handler function
AddHandler oCommandDef.OnExecute, Sub(sender As Object, e As EventArgs) CallMethod(oCommand.Handler)
Next
End Sub

Private Sub CallMethod(name As String)
Me.GetType().GetMethod(name).Invoke(Me, Nothing)
End Sub

Sub DrawLogo()
' [My add-in's code to draw logo in Inventor]
End Sub

Sub PublishDrawing()
' [My add-in's code to publish drawing in Inventor]
End Sub

关于VB.NET: "AddHandler"通过将 Handler 函数指定为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55516872/

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