繁体   English   中英

(Visual Studio 2019 社区)当我打开想要打开设计器时创建窗口句柄时出错

[英](Visual Studio 2019 Community) Error Creating Window Handle when I open want open the designer

我使用的是 Visual Studio 2019 社区版,我的程序是一个很大的 winform 应用程序。 当我尝试打开 winforms 进行编辑时,我总是收到错误消息:

Error creating window handle.

bei System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
bei System.Windows.Forms.Control.CreateHandle()
bei System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
bei System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
bei System.Windows.Forms.Control.CreateControl()
bei System.Windows.Forms.Control.ControlCollection.Add(Control value)
bei System.Windows.Forms.Form.ControlCollection.Add(Control value)
bei System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)  

我不知道如何修复它,也无法弄清楚导致此错误的原因。 在我的表单中,有一个面板被注入到另一个面板中。

编辑:我可以在没有错误的情况下构建和启动应用程序,但只有 Visual Studio 中的设计器不工作。

Edit2:我可以打开所有没有面板的窗口,如果它们有一个,我会收到错误消息。

Edit3:有时设计者会写出所有的控件和变量都没有声明或没有分配。

我建议将所有表单的运行时代码移动到一个单独的方法中,该方法通过this.Load执行并在设计时使用布尔属性阻止...

this.Load += delegate {
    if(!THISISLOADED) return; //Exits -> will always be false in Design Time  
    OnLoad();
};

private void OnLoad()
{
   //place all run time code here - will not execute at design time 
   //...
   THISISLOADED = true; //just in case 
}


private bool m_THISISLOADED = false; //false at design time
public bool THISISLOADED {
    get { return m_THISISLOADED; }
    set {
        m_THISISLOADED = value;
    }
}

然后就在显示表单之前,将布尔属性 THISISLOADED 设置为 true...

myWinForm.THISISLOADED = true;
myWinForm.ShowDialog();

编辑:刚刚从控件的构造函数中找到了这个检测设计模式

您可以使用LicenseManager测试设计模式...

this.Load += delegate {
    if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) return;
    OnLoad();
};

我现在发现了错误。 我有包含防止闪烁的代码。 此代码在创建窗口句柄之前执行,这就是导致此错误的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM