我处于疯狂的边缘...
在我实际构建的应用程序中,我正在处理两个需要相互交互的动态添加控件,但我已将问题简化为一个尽可能简单的示例在静态加载控件的情况下,它仍然存在相同的问题:调用委托时出现 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
控制 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
如果我在分配 Control1 委托的 Control2 行上放置一个断点,我可以单步执行并观察它在 Control1 对象中的设置。但是,当 btnButton_Click 事件触发时,_delClicked 的值又变回了 Nothing。
任何帮助将不胜感激。谢谢!