2013-06-27 47 views
5

我在Windows CE應用程序中打開繼承的表單時遇到問題。這是一個我從前僱員手中接過的項目,但是有些編譯版本在某些移動設備上運行,所以我認爲它應該能夠打開。我有正確的VS版本(2008),並嘗試清理解決方案並重新構建解決方案。部署解決方案時,它的作用就像一個魅力。當我試圖進入的繼承形式的設計師,我得到以下錯誤:在繼承的表單上打開設計器時出錯

To prevent possible data loss before loading the designer, the following errors must be resolved: 

Object reference not set to an instance of an object. 

堆棧跟蹤:

at MyApp.frmBase.UpdateOnline() in C:\Users\Corne\Documents\Visual Studio 2008\Projects\Test\MyApp\MyApp\frmBase.cs:line 35 
at MyApp.frmBase.frmBase_Load(Object sender, EventArgs e) in C:\Users\Corne\Documents\Visual Studio 2008\Projects\Test\MyApp\MyApp\frmBase.cs:line 30 
at System.Windows.Forms.Form.OnLoad(EventArgs e) 
at System.Windows.Forms.Form.OnCreateControl() 
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) 
at System.Windows.Forms.Control.CreateControl() 
at System.Windows.Forms.Control.SetVisibleCore(Boolean value) 
at System.Windows.Forms.Form.SetVisibleCore(Boolean value) 
at System.Windows.Forms.Control.set_Visible(Boolean value) 
at System.Windows.Forms.Design.DesignerFrame.Initialize(Control view) 
at System.Windows.Forms.Design.DocumentDesigner.Initialize(IComponent component) 
at System.Windows.Forms.Design.FormDocumentDesigner.Initialize(IComponent component) 
at System.ComponentModel.Design.DesignerHost.AddToContainerPostProcess(IComponent component, String name, IContainer containerToAddTo) 
at System.ComponentModel.Design.DesignerHost.Add(IComponent component, String name) 
at System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IDesignerHost.CreateComponent(Type componentType, String name) 
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(Type type, ICollection arguments, String name, Boolean addToContainer) 
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.System.ComponentModel.Design.Serialization.IDesignerSerializationManager.CreateInstance(Type type, ICollection arguments, String name, Boolean addToContainer) 
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration) 
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager) 
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager) 
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload) 

回答

8

你可以清楚地從堆棧跟蹤看到你的基本形式的加載事件處理程序正在運行並引發異常。這是正常的,像Load和Paint這樣的基本形式的事件也在設計時運行。它提供了所見即所得的設計師視圖。但是,如果該代碼只能在運行時正常工作,則效果不佳。

Control.DesignMode屬性旨在爲您提供一種方法來檢查類中的代碼是否在設計時運行。不幸的是,它在CF中不可用,因此需要採用不同的方法。神奇的咒語看起來像這樣:

private void frmBase_Load(object sender, EventArgs e) { 
     if (this.Site == null || !this.Site.DesignMode) { 
      // Not in design mode, okay to do dangerous stuff... 
      this.UpdateOnline(); 
     } 
    } 
+1

超大工作非常好,就像一個魅力! –