通过子程序引发事件
Raising an event through subroutine
我正在尝试通过子例程引发一个事件,以通知我程序的一些观察者动画过渡已完成。但它告诉我它不能直接调用,我需要使用 RaiseEvent。我尝试添加处理程序,但它仍然不起作用。我该怎么办?
Utility.raiseEventTest(Me.TransitionCompletedEvent, Me, New Transition.Args())
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())
所有订阅者都会收到通知并收到事件。
我正在尝试通过子例程引发一个事件,以通知我程序的一些观察者动画过渡已完成。但它告诉我它不能直接调用,我需要使用 RaiseEvent。我尝试添加处理程序,但它仍然不起作用。我该怎么办?
Utility.raiseEventTest(Me.TransitionCompletedEvent, Me, New Transition.Args())
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())
所有订阅者都会收到通知并收到事件。