在继承的表单上打开设计器时出错

时间:2013-06-27 09:03:16

标签: c# windows-ce windows-forms-designer

我在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)  

2 个答案:

答案 0 :(得分:9)

您可以从堆栈跟踪中清楚地看到您的基本表单的Load事件处理程序正在运行并引发异常。这是正常的,基本形式的事件如Load和Paint也在设计时运行。它提供了WYSIWYG设计师视图。但是,如果该代码只能在运行时正常工作,那么这很糟糕。

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 :(得分:-1)

我遇到了同样的问题。我通过向子窗体构造函数添加 base() 解决了我的问题。

public partial class FormChildren : ParentForm
{
    public FormChildren() : base()
    {
        InitializeComponent();
    }
}

这个答案很好:

Windows Form inheritance