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

标签 vb.net event-handling

我正在为 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/

相关文章:

Delphi OnClick 多个单元的问题

javascript - 仅在第一次使用 javascript 时滚动页面时如何提醒?

vb.net - 在一个数据 GridView 中显示父数据表并在另一个数据 GridView 中显示子数据表元素?

mysql - 显示多个值(value)成员(member)

asp.net - VB 的 Razor View 不支持 @Model?

EventHandler 创建的 C# Windows 窗体立即消失

c# - 为什么不使用自定义类而不是继承 EventArgs 类

c# - 从十进制转换时,字符串包含尾随零

wpf - 如何为不同的数据模板设置窗口位置

python - 绘制 networkx.Graph : how to change node position instead of resetting every node?