Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET:“您必须遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他有关法律法规。”;AddHandler";通过将Handler函数指定为变量_Vb.net_Event Handling - Fatal编程技术网

VB.NET:“您必须遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他有关法律法规。”;AddHandler";通过将Handler函数指定为变量

VB.NET:“您必须遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他有关法律法规。”;AddHandler";通过将Handler函数指定为变量,vb.net,event-handling,Vb.net,Event Handling,我正在为Autodesk Inventor(一个三维CAD程序)编写外接程序。外接程序将一组内部命令添加到Inventor,并为每个命令创建一个工具栏按钮。我需要将这些命令的执行事件连接到外接程序中的处理程序函数 我创建了一个“MyCommand”类,它包含定义命令所需的所有信息。我还创建了一个(MyCommand)列表,其中包含每个命令的定义。最后,我可以遍历每个命令,创建内部Inventor命令定义,并将按钮添加到工具栏。但是,我不知道如何将命令与其循环中的处理函数相关联 下面的示例代码说明

我正在为Autodesk Inventor(一个三维CAD程序)编写外接程序。外接程序将一组内部命令添加到Inventor,并为每个命令创建一个工具栏按钮。我需要将这些命令的执行事件连接到外接程序中的处理程序函数

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

下面的示例代码说明了我的目标:

Sub-CreateCommands()
Dim oCommand作为新列表(MyCommand)
添加(新的MyCommand(“DrawLogoCmd”、“DrawLogo”、“DrawLogoSub”))
添加(新的MyCommand(“PublishDrawingCmd”、“PublishDrawingCmd”、“PublishDrawingSub”))
"(十几条命令)
对于每个oCommand作为oCommand中的MyCommand
'用于将内部命令定义和按钮添加到Inventor的代码。这很好用。
Dim oCommandDef作为Inventor.CommandDefinition=InventorApp.CommandDefinitions.Add(oCommand.InternalDefinitionName,oCommand.DisplayName)
InventorApp.ToolbarButtons.Add(oCommandDef)
'将命令定义与处理程序函数关联
’=======这是我需要弄清楚的一条线=====
AddHandler oCommandDef.OnExecute,oCommand.HandlerSubName的地址
下一个
端接头
Sub-DrawLogoSub()
“[我的外接程序在Inventor中绘制徽标的代码]
端接头
子出版物绘图子目录()
“[我的外接程序在Inventor中发布图形的代码]
端接头
类MyCommand
公共InternalDefinitionName作为字符串
作为字符串的公共显示名
公共HandlerSubName作为字符串
Sub New(InternalDefinitionName为字符串,DisplayName为字符串,HandlerSubName为字符串)
和我一起
.InternalDefinitionName=InternalDefinitionName
.DisplayName=DisplayName
.HandlerSubName=HandlerSubName
以
端接头
末级
那么,这可能吗?是否有某种方法可以使用函数名作为字符串来获取函数的“AddressOf”?或者,是否有某种方法将对函数本身的引用存储在MyCommand类中,并将其传递给AddressOf运算符

或者,除了“AddHandler/AddressOf”之外,还有其他方法可以使用吗


非常感谢您的建议。

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

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
,而不是将方法名称硬编码为字符串文本,只是为了更安全):


您希望代码将字符串映射到方法引用。我不再100%清楚语法,但它可能看起来像这样:

Dim actionMap As Dictionary(Of string, Action(Of Object, EventArgs))
actionMap.Add("DrawLogo", AddressOf DrawLogo)
actionmap.Add("PublishDrawing", AddressOf PublishDrawing)
For Each oCommand As MyCommand In oCommands
    Dim oCommandDef As Inventor.CommandDefinition = InventorApp.CommandDefinitions.Add(oCommand.InternalDefinitionName, oCommand.DisplayName)
    InventorApp.ToolbarButtons.Add(oCommandDef)

    AddHandler oCommandDef.OnExecute, actionMap(oCommand.HandlerSubName)
Next
然后每个循环的
可以像这样使用字典:

Dim actionMap As Dictionary(Of string, Action(Of Object, EventArgs))
actionMap.Add("DrawLogo", AddressOf DrawLogo)
actionmap.Add("PublishDrawing", AddressOf PublishDrawing)
For Each oCommand As MyCommand In oCommands
    Dim oCommandDef As Inventor.CommandDefinition = InventorApp.CommandDefinitions.Add(oCommand.InternalDefinitionName, oCommand.DisplayName)
    InventorApp.ToolbarButtons.Add(oCommandDef)

    AddHandler oCommandDef.OnExecute, actionMap(oCommand.HandlerSubName)
Next


在这里,我将添加从VB6/VBScript到.Net的移动,微软改变了他们对VB的风格指导。他们不再推荐像
o
这样的匈牙利疣,而是根据.Net更强烈地使用类型和更好的工具支持的方式,针对这些疣专门推荐。前缀不会增加以前的值,现在您可以只编写
command
,而不是
oCommand
,这样更易于阅读。

非常好的建议,谢谢。我正在尝试第一个,但在
AddHandler-oCommandDef.OnExecute,oCommand.Handler
行中,
oCommand.Handler
部分给出以下错误:“类型为'EventHandler'的值无法转换为'ButtonDefinitionSink\u OnExecuteEventHandler'。我尝试使用“Sink”类型而不是“EventHandler”,但它没有定义。我找不到任何关于它的文档,无论是在vb.net文档中还是在Inventor中。有什么想法吗?在对象浏览器中搜索。它必须是在一个Audodesk库中作为公共类型公开的自定义委托类型。如果找不到它,请查看
Inventor.CommandDefinition
类中
OnExecute
事件的定义,它应该会显示它的完全限定名。谢谢,我在Inventor节点正下方的对象浏览器中找到了它。我不知道为什么会列在那里,但IntelliSense不会把它提出来。。。。无论如何,这看起来是可行的(或者至少是编译)。如果它运行正常,我会发回这里。同时,你能解释一下为什么不建议使用反射吗?可能导致什么样的问题或不良行为?反射会绕过编译器的类型检查。换句话说,如果您更改了方法的名称,但忘记更改字符串文字,它仍然可以正常编译,但在运行时会抛出异常。它正在工作!我很激动。这将使添加命令和按钮变得更加容易。我不得不调整一两件事,比如在
oCommands
声明中添加
WithEvents
。但在此之后,我可以通过定义按钮的内部名称、显示名称和处理程序子项来添加多个按钮。非常感谢您的帮助。感谢字典的建议。史蒂文的建议需要更少的开销,所以我将首先贯彻到底。如果不行的话,我就试试字典