研究告诉我,从构造函数本身引发事件是不可行的,因为对象可能没有完全初始化......那么构造函数触发后我可以在哪里触发事件?
您可以做的一件事是添加一个方法来处理其他后期任务:
Friend Class FooBar
Public Sub New
' your code here
End Sub
Public Sub Create
' do anything you want
End Sub
End Class
别处:
Friend WithEvents Foo As Foobar
' ...
Foo = New FooBar ' Foo doesnt exist until ctor code executes and the
' code returns to here.
Foo.Create ' do whatever you like, as long as any other
' objects referenced have been created.
从 ctor 调用 sub 来引发事件不适用于类的原因是:
Private Sub SomeEvent(sender As Object, e As EventArgs) Handles Foo.SomeEvent
Console.Beep()
End Sub
关键是Handles Foo.SomeEvent
没有Foo
尚未处理该事件。它不会崩溃并且引发事件,但没有对象供侦听器捕获/处理事件。创建了足够的表单InitializeComponents
,它确实适用于表单。
可能还有一个接口来实现类似的东西,我知道一些组件,但不知道类。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)