通过子程序

时间:2017-06-11 14:02:36

标签: vb.net subroutine addhandler

我正在尝试通过子程序引发一个事件来通知我程序的一些观察者已经完成了动画过渡。但它告诉我它无法直接调用,我需要使用RaiseEvent。我尝试添加处理程序,它仍然无法正常工作。我该怎么办?

Utility.raiseEventTest(Me.TransitionCompletedEvent, Me, New Transition.Args())

https://prntscr.com/fiholb

 Public Shared Sub raiseEventTest(Of T As System.EventArgs)(theEvent As EventHandler(Of T), sender As Object, args As T)
        If theEvent Is Nothing Then
            Return
        End If
        '
        For Each handler As EventHandler(Of T) In theEvent.GetInvocationList()
            Try
                Dim target As ISynchronizeInvoke = TryCast(handler.Target, ISynchronizeInvoke)
                If target Is Nothing OrElse target.InvokeRequired = False Then
                    handler(sender, args)
                Else
                    target.BeginInvoke(handler, New Object() {sender, args})
                End If
            Catch generatedExceptionName As Exception
            End Try
        Next
    End Sub

1 个答案:

答案 0 :(得分:1)

按照建议使用RaiseEvent,您无需使用该类型的代码......

变化:

Utility.raiseEventTest(Me.TransitionCompletedEvent, Me, New Transition.Args())

要:

RaiseEvent TransitionCompleted(Me, New Transition.Args())

所有订阅者都会收到通知并接收活动。