VB.NET 事件地址 [英] VB.NET Event addressof

查看:26
本文介绍了VB.NET 事件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个回调,由不同的类在我的类中引发:

I would like to set up a callback that would be raised in my class by a different class:

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 不喜欢我附加和分离事件的方式.有人能告诉我怎么做才能正确吗?

But VB.NET does not like the way I attach and detach the events. Can somebody tell me how to do it correctly?

非常感谢您的帮助!

推荐答案

这看起来就像您通常使用 C#.在 VB.NET 中,您使用 AddHandler 语句:

That looks as if you're normally using C#. In VB.NET you use the AddHandler statement:

AddHandler m_camera.NewFrame, AddressOf camera_NewFrame

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

To remove the handler use RemoveHandler

RemoveHandler m_camera.NewFrame, AddressOf camera_NewFrame

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

However, if the event handler is in a different class it need to be public:

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

并且您需要一个实例:

AddHandler Camera.NewFrame, AddressOf m_camera.camera_NewFrame

这篇关于VB.NET 事件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆