vb.net,不能直接调用事件

Jos*_*oie 1 vb.net asp.net event-handling

我有一段代码:

innerchannel.Closing += Function(sender, e)
                        RaiseEvent Closing(sender, e)

                        End Function
Run Code Online (Sandbox Code Playgroud)

有一个问题

innerchannel.Closing 
Run Code Online (Sandbox Code Playgroud)

VisualStudio告诉我:

Public Event Closing(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
Run Code Online (Sandbox Code Playgroud)

如何修复此工作?

Tim*_*ter 7

我假设您要为Closing-event添加处理程序:

AddHandler innerchannel.Closing, AddressOf Closed
Run Code Online (Sandbox Code Playgroud)

如果要引发自定义事件,例如UserControl:

Class Channel
    Inherits UserControl
    Public Event Closing(ch As Channel)

    ' this could be for example a button-click handler
    Protected Sub Closed(sender As Object, e As System.EventArgs)
        RaiseEvent Closing(Me)
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)


GSe*_*erg 6

您不能+=在VB中使用添加事件处理程序.你用AddHandler.

您的代码尝试调用Closing它就好像它是一个函数并对其结果执行添加.