我正在尝试通过子例程引发一个事件,以通知我的程序的一些观察者动画过渡已完成。但它告诉我它不能直接调用,我需要使用 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
只需按照建议使用 RaiseEvent 即可,无需使用这种类型的代码...
改变:
Utility.raiseEventTest(Me.TransitionCompletedEvent, Me, New Transition.Args())
自:
RaiseEvent TransitionCompleted(Me, New Transition.Args())
所有订阅者都将收到通知并接收活动。