继承窗体打开设计器时出错。

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)  
2个回答

10

从堆栈跟踪中可以清楚地看到,您的基本表单的Load事件处理程序正在运行并抛出异常。这是正常的,像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

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

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

这个答案很好:

Windows窗体继承


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接