2013-06-27 36 views
5

Tôi đang gặp sự cố khi mở biểu mẫu được kế thừa trong ứng dụng Windows CE. Đây là một dự án mà tôi đang tiếp nhận từ một nhân viên cũ, nhưng có các phiên bản được biên dịch chạy trên một số thiết bị di động vì vậy tôi cho rằng nó sẽ có thể mở. Tôi có phiên bản VS chính xác (2008) và đã thử làm sạch giải pháp và xây dựng lại giải pháp. Khi triển khai giải pháp nó hoạt động như một sự quyến rũ. Ngay sau khi tôi cố gắng đi vào nhà thiết kế trong các hình thức kế thừa, tôi nhận được lỗi sau:Lỗi khi mở trình thiết kế trên biểu mẫu được kế thừa

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. 

Stack Trace:

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) 

Trả lời

8

Bạn có thể thấy rõ từ stack trace rằng hình thức cơ sở của bạn của Load event handler đang chạy và ném một ngoại lệ. Điều này là bình thường, các sự kiện ở dạng cơ bản như Load và Paint chạy ở thời gian thiết kế là tốt. Nó cung cấp khung nhìn thiết kế WYSIWYG. Điều này tuy nhiên đi kém nếu mã đó chỉ có thể hoạt động bình thường khi chạy.

Thuộc tính Control.DesignMode có nghĩa là cung cấp cho bạn cách kiểm tra xem mã trong lớp có hoạt động ở thời gian thiết kế hay không. Thật không may là nó không có sẵn trong CF nên cần một cách tiếp cận khác. Câu thần chú kỳ diệu trông như thế này:

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

TUYỆT VỜI TUYỆT VỜI TUYỆT VỜI! –

Các vấn đề liên quan