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)  
4

2 回答 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();
        }
    }
于 2013-06-27T10:08:44.190 回答
-1

我有同样的问题。我通过将 base() 添加到子表单构造函数来解决我的问题。

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

这个答案很好:

Windows 窗体继承

于 2021-01-23T13:26:15.037 回答