1

我想设置一个回调,该回调将由另一个班级在我的班级中引发:

Public Class CameraWindow
        Inherits System.Windows.Forms.Control
        Private m_camera As Camera = Nothing

        ' Camera property
        <Browsable(False)> _
        Public Property Camera() As Camera
            Get
                Return m_camera
            End Get
            Set(value As Camera)
                ' lock
                Monitor.Enter(Me)

                ' detach event
                If m_camera IsNot Nothing Then
                    m_camera.NewFrame -= New EventHandler(AddressOf camera_NewFrame)
                    timer.[Stop]()
                End If

                m_camera = value
                needSizeUpdate = True
                firstFrame = True
                flash = 0

                ' atach event
                If m_camera IsNot Nothing Then
                    m_camera.NewFrame += New EventHandler(AddressOf camera_NewFrame)
                    timer.Start()
                End If

                ' unlock
                Monitor.[Exit](Me)
            End Set
        End Property

        ' On new frame ready
        Private Sub camera_NewFrame(sender As Object, e As System.EventArgs)
            Invalidate()
        End Sub

事件定义在

  Public Class Camera

        Public Event NewFrame As EventHandler

但是 VB.NET 不喜欢我附加和分离事件的方式。有人可以告诉我如何正确地做到这一点吗?

非常感谢你的帮助!

4

1 回答 1

4

看起来好像您通常使用 C#。在 VB.NET 中,您使用以下AddHandler语句

AddHandler m_camera.NewFrame, AddressOf camera_NewFrame

要删除处理程序,请使用RemoveHandler

RemoveHandler m_camera.NewFrame, AddressOf camera_NewFrame

但是,如果事件处理程序在不同的类中,它需要是公共的:

Public Sub camera_NewFrame(sender As Object, e As System.EventArgs)
    Invalidate()
End Sub

你需要一个实例:

AddHandler Camera.NewFrame, AddressOf m_camera.camera_NewFrame
于 2014-03-06T08:24:58.110 回答