未将对象引用设置到对象的实例 报错的解决办法!!!!?????
我的工程形式如下:(工程用VS2008开发)
一个WPF工程,一个WindowsForm工程
想在WPF工程中点击按钮,调用WindowsForm工程的方法
WPF工程的代码是这样的:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim daha As New EyeShotControl.UserControl1
daha.AddModel()
End Sub
WindowsForm工程代码是这样的:
Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Sub AddModel()
' initialiazes the plane
xyPlane = Plane.XY
'尝试在控件中增加一个模型,代码来源于帮助文档
' creates a new Quad entity
Dim Q As New Quad(0, 0, 0, 100, 0, 0, 100, 80, 80, 0, 80, 80, Black)
' adds the entity to viewport's master entity collection
Me.ViewportProfessional1.Entities.Add(Q)
End Sub
如上所述,如果这样写代码,当运行到红色字体的代码的时候就会报错:“未将对象引用设置到对象的实例”。
但是,我如果做如下两种方式的任何一种更改都会没有报错:
方式1,AddModel()的代码直接在Load中运行,即:
Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'' initialiazes the plane
xyPlane = Plane.XY
''尝试在控件中增加一个模型,代码来源于帮助文档
'' creates a new Quad entity
Dim Q As New Quad(0, 0, 0, 100, 0, 0, 100, 80, 80, 0, 80, 80, Black)
'' adds the entity to viewport's master entity collection
Me.ViewportProfessional1.Entities.Add(Q)
End Sub
Public Sub AddModel()
' initialiazes the plane
'xyPlane = Plane.XY
'尝试在控件中增加一个模型,代码来源于帮助文档
' creates a new Quad entity
'Dim Q As New Quad(0, 0, 0, 100, 0, 0, 100, 80, 80, 0, 80, 80, Black)
' adds the entity to viewport's master entity collection
'Me.ViewportProfessional1.Entities.Add(Q)
End Sub
方式2,在Load中直接调用AddModel(),代码即:
Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddModel()
End Sub
Public Sub AddModel()
' initialiazes the plane
xyPlane = Plane.XY
'尝试在控件中增加一个模型,代码来源于帮助文档
' creates a new Quad entity
Dim Q As New Quad(0, 0, 0, 100, 0, 0, 100, 80, 80, 0, 80, 80, Black)
' adds the entity to viewport's master entity collection
Me.ViewportProfessional1.Entities.Add(Q)
End Sub
我的问题是:为什么执行代码放置在AddModel()方法中就不能运行,但是同样的代码放置在Load中或者由Load调用就可以正常运行呢?
试了多次,都是这样。往大家多多帮忙啊。谢谢。