我快要疯了...
实际上我正在开发的应用程序中,我需要处理两个动态添加的控件,这些控件需要相互交互,但我已经将问题简化为一个最简单的静态加载的示例,并且仍然出现同样的问题:在调用委托时出现NullReferenceException异常。以下是要点:
控件 1
Partial Class Control1
Inherits System.Web.UI.UserControl
Private _delClicked As System.Delegate
Public WriteOnly Property UpdateLabel() As System.Delegate
Set(ByVal value As System.Delegate)
_delClicked = value
End Set
End Property
Protected Sub btnButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnButton.Click
Dim aObj(0) As Object
aObj(0) = 1
_delClicked.DynamicInvoke(aObj)
End Sub
End Class
Control 2
Partial Class Control2
Inherits System.Web.UI.UserControl
Protected WithEvents Control1 As New Control1
Delegate Sub ChangeLabel(ByVal int As Integer)
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Dim delChangeLabel As New ChangeLabel(AddressOf UpdateLabel)
Me.Control1.UpdateLabel = delChangeLabel
End Sub
Private Sub UpdateLabel(ByVal int As Integer)
lblLabel.Text = "Value is now " & int
End Sub
End Class
如果我在Control2的代码行上设置断点,这里会将Control1委托赋值,我可以逐步执行并观察它在Control1对象中被设置。但是,当btnButton_Click事件触发时,_delClicked的值已经变成了Nothing。
非常感谢您的帮助。谢谢!