【问题标题】:Raising an event through subroutine通过子程序引发事件
【发布时间】:2017-06-11 14:02:36
【问题描述】:

我正在尝试通过子例程引发一个事件,以通知我的一些程序的观察者动画转换已完成。但它告诉我它不能直接调用,我需要使用 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

【问题讨论】:

标签: vb.net subroutine addhandler


【解决方案1】:

按照建议使用RaiseEvent,你不需要使用那种类型的代码...

变化:

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

收件人:

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

所有订阅者都将收到通知并接收事件。

【讨论】:

  • 如此简单。谢谢楼主!
猜你喜欢
  • 2014-03-20
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多