【发布时间】:2019-08-26 06:20:55
【问题描述】:
我正在为 Autodesk Inventor(一个 3D CAD 程序)编写插件。该插件将一堆内部命令添加到 Inventor,并为每个命令创建一个工具栏按钮。我需要将每个命令的 Execute 事件连接到我的加载项中的处理程序函数。
我创建了一个“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”之外还有其他方法可以吗?
欢迎提出任何建议。
【问题讨论】:
标签: vb.net event-handling