- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 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/
我正在尝试使用 Widget.addHandler()。但是,处理程序永远不会被调用。下面是我的示例代码。我需要更改什么才能解决此问题? 我的处理程序实现: public class CustomMo
动态附加/分离事件处理程序有优势吗? 手动分离处理程序是否有助于确保没有剩余对已处置对象的引用? 最佳答案 我很确定 Handles 子句只是语法糖,并将 AddHandler 语句插入到您的构造函数
我正在运行 OS X Lion 和 PHP 5.3.6 的 Mac 上工作,并尝试了 AddType、AddHander 和 AllowOverride已在 httpd.conf 中设置为 ALL;然
我对 Vb.Net 事件和处理程序很满意。谁能帮助我了解如何在 C# 中创建事件处理程序并引发事件。 最佳答案 只懂C#,或者只懂VB.Net的开发者,可能不知道这是VB.NET和C#最大的区别之一。
我开始学习 strophe 库的使用,当我使用 addHandler 解析响应时,它似乎只读取 xml 响应的第一个节点,所以当我收到这样的 xml 时:
我目前正在尝试通过 Heinrich Apfelmus 的 reactive-banana 了解 FRP ,与我看过的其他库相比,它似乎是一个文档齐全且简单的库。 但是,我无法理解 AddHandle
如前所述 here , PropertyChangedEventManager类(class) Provides a WeakEventManager implementation so that y
使用AddHandler方法,如果我从不使用RemoveHandler,在某些条件和情况下会导致内存泄漏吗?我不太确定这件事的真实性。 还有其他导致内存泄漏的原因吗?这些原因仅在 VB 中存在,而在
我正在将 WPF 应用程序移植到 Windows Universal。我想将相同的 MouseLeftButtonDown 处理程序添加到我的控件中。它们似乎已被 Tapped Event 取代。但是
我想添加 AddHandler 来为 .jpg 文件运行 php 代码, 当我在 /etc/apache2/mods-available/mime.conf 添加这个 AddHandler appli
我正在尝试动态创建 dropdownList框,我想尝试添加 AddHandlers给他们,以便在其中选择一个项目时,它会触发一个事件,但还需要传递另一个变量,我不知道该将什么作为 system.Ev
我正在使用 AddHandler 将函数连接到我动态创建的控件事件: Public Delegate Sub MyEventHandlerDelegate(ByVal sender As Object
我正在为 Autodesk Inventor(一种 3D CAD 程序)编写插件。该插件向 Inventor 添加了一堆内部命令,并为每个命令创建了一个工具栏按钮。我需要将每个命令的执行事件连接到我的
我在 Site5 托管我的临时和生产服务器,在我看来,Site5 是一个相对较好的托管商。问题不在于它们的质量,而在于与 PHP 版本相关的问题。 我们的开发服务器使用的是 PHP 5.3,这是一个很
最新版本的 RestSharp v106.6.9 显然做了一些更改,使得 Request 的 AddHandler 方法的重写过时,例如此签名: [Obsolete("Use the overload
我使用“TextWebSocketHandler”作为 websockets 和“WebSocketConfigurer”来配置 websocket。 我有一个场景,需要对处理程序的不同实例进行处理。
根据 UIElement.AddHandler on MSDN ,您可以为 handledEventsToo 传入一个 bool 值参数,因此您仍然可以收到已处理事件的通知。是否有与此等效的 XAML
从事件创建可观察对象时,看起来以下是最常见的方式: var o = Observable.FromEventPattern(h => source.Event += h,
问题的第一部分:这 2 个事件注册之间有什么区别? _popUp.AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHan
UIElement类定义静态 RoutedEvent 成员 MouseLeftButtonDownEvent 和 MouseLeftButtonUpEvent - 但没有 MouseMoveEvent
我是一名优秀的程序员,十分优秀!