首页 > 解决方案 > VB.NET 使用 Delegate 添加任何 EventHandler 类型

问题描述

我发现了一些我认为可以解决我的问题的帖子(不是很多),但在我所有的阅读中我仍然没有解决方案。

我要做的基本上是创建一个方法,该方法将使用Reflection.EventInfoand将任何给定控件的事件绑定到任何给定对象的方法Reflection.MethodInfo。我正在使用 Winforms,我很想只使用 WPF,但不幸的是,这不是一个选择。

我有一个抽象BoundControl类,它只是一个用于继承控件的空画布。在该类中是以下函数:

Public Sub CallMethod(eventName As String, sender As Object, e As EventArgs)
...
End Sub

每当引发给定控件的事件时,这就是我想要调用的。该方法中的逻辑在数据上下文中调用正确的方法(我已将其设置为模仿 WPF)。这工作正常,我的问题实际上是将上述方法绑定到控件的事件。

我使用下面的方法绑定(与上面的方法在同一个类中)。请注意,我已经删除了不重要的逻辑,例如我的自定义绑定标记类以及与我的问题无关的任何其他内容:

Public Sub SetEventBind(ByRef ctrl as Control)
    Dim ctrlStr As String = "EventName"
    Dim ctrlEvent as Reflection.EventInfo = ctrl.GetType.GetEvent(ctrlStr)
    Dim eh As EventHandler = (Sub(sender, e) CallMethod(ctrlStr, sender, e))
    ctrlEvent.AddEventHandler(ctrl, eh)
End Sub

我正在尝试在事件上运行我的代码,LinkLabelLinkClicked我希望它适用于任何控件的事件。最终发生的是EventHandlertype 无法转换为LinkLabelLinkClickedEventHandler. 所以只是为了测试我尝试了下面的代码,它确实有效:

Public Sub SetEventBind(ByRef ctrl as Control)
    Dim ctrlStr As String = "EventName"
    Dim ctrlEvent as Reflection.EventInfo = ctrl.GetType.GetEvent(ctrlStr)
    Dim eh As LinkLabelLinkClickedEventHandler = (Sub(sender, e) CallMethod(ctrlStr, sender, e))
    ctrlEvent.AddEventHandler(ctrl, eh)
End Sub

但问题是LinkLabelLinkClickedEventHandler不适用于按钮单击或复选框选中的更改。我也尝试了下面的代码,但没有成功:

Public Sub SetEventBind(ByRef ctrl as Control)
    Dim ctrlStr As String = "EventName"
    Dim ctrlEvent as Reflection.EventInfo = ctrl.GetType.GetEvent(ctrlStr)
    Dim eh As [Delegate] = [Delegate].CreateDelegate(ctrlevent.EventHandlerType, Me, (Sub(sender, e) CallMethod(ctrlStr, sender, e)).Method)
    ctrlEvent.AddEventHandler(ctrl, eh)
End Sub

我想我的问题是多部分的。我想如果我可以动态地创建一个与我相同类型的委托,ctrlEvent.EventHandlerType那么我可以让它工作。是否可以动态设置变量的类型?如果没有,是否有另一种方法可以将任何控件的事件动态绑定到方法?

标签: vb.neteventsreflectiondelegateseventhandler

解决方案


我发现了一篇很有帮助的文章(如下)。我必须做的是创建一个单独的函数,它将委托转换并返回正确的委托类型。

Public Sub SetEventBind(ByRef ctrl As IBoundControl, pBindingTag As BindingTag, pDoAdd As Boolean)
    If pBindingTag.BindingType <> BindType.EventBind Then Exit Sub

    Dim objStr As String = pBindingTag.DataContextBindName
    Dim ctrlStr As String = pBindingTag.ControlBindName

    If Not (String.IsNullOrEmpty(objStr) OrElse String.IsNullOrEmpty(objStr)) Then
        Dim ctrlEvent As Reflection.EventInfo = ctrl.GetType.GetEvent(ctrlStr)

        If Not ctrlEvent Is Nothing Then
            Dim eventDel As [Delegate] = Sub(sender, e)
                                             CallMethod(ctrlStr, sender, e)
                                         End Sub

            Dim convertedDel = CastDelegate(eventDel, ctrlEvent.EventHandlerType)

            ctrlEvent.RemoveEventHandler(ctrl, convertedDel)
            If pDoAdd Then ctrlEvent.AddEventHandler(ctrl, convertedDel)
        End If
    End If
End Sub

Private Function CastDelegate(source As [Delegate], type As Type) As [Delegate]
    Dim delegates As [Delegate]() = source.GetInvocationList()
    Return [Delegate].CreateDelegate(type, delegates(0).Target, delegates(0).Method)
End Function

可以在此处找到帮助的文章: 铸造代表


推荐阅读