简体   繁体   English

表格 inheritance 崩溃,.NET 6

[英]Form inheritance crashes with .NET 6

I'm converting an old winforms application from 4.8 to 6.0.1.我正在将旧的 winforms 应用程序从 4.8 转换为 6.0.1。 The conversion has been incredibly easy and the application worked with no problems at all until, suddenly, Visual Studio (2022 64-bit v17.0.5) refuses to show the form designer for every form inherits from another.转换非常简单,应用程序完全没有问题,直到突然,Visual Studio(2022 64 位 v17.0.5)拒绝向表单设计器显示从另一个表单继承的每个表单。

I scaled down my project until I discovered that it wasn't me...我缩小了我的项目,直到我发现它不是我......

using System.Windows.Forms;

namespace MyApp
{
    public partial class BaseForm : Form
    {
        public BaseForm()
        {
            InitializeComponent();
        }
    }
}

and

namespace MyApp
{
    public partial class ChildForm : BaseForm
    {
        public ChildForm()
        {
            InitializeComponent();
        }
    }
}

The other portion of every class (winforms splits the form class in two partials) are perfectly untouched.每个 class 的其他部分(winforms 将表格 class 拆分为两个部分)完全不变。

The two forms are completely empty but the designer refuses to draw ChildForm returning this nice and senseless message:两个 forms 完全是空的,但设计者拒绝绘制 ChildForm 返回这个漂亮而无意义的消息:

The designer could not be shown for this file because none of the classes within it can be designed.无法为此文件显示设计器,因为无法设计其中的任何类。 The designer inspected the following classes in the file: The designer could not be shown for this file because none of the classes within it can be designed.设计器检查了文件中的以下类: 无法为该文件显示设计器,因为其中没有任何类可以设计。 The designer inspected the following classes in the file: \r\n ChildForm --- The base class 'MyApp.BaseForm' could not be loaded.设计器检查了文件中的以下类:\r\n ChildForm --- 无法加载基础 class 'MyApp.BaseForm'。 Ensure the assembly has been referenced and that all projects have been built.确保已引用程序集并且已构建所有项目。 在此处输入图像描述

I've already tried everything I found on the Internet:我已经尝试了在 Internet 上找到的所有内容:

  • clean and rebuild the solution清理并重建解决方案
  • exit from VS退出 VS
  • put an explicit (and useless) :base() instruction on the ctor of the two forms在两个 forms 的 ctor 上放置一个显式(且无用)的:base()指令
  • empty the cryptic folder C:\Users[me]\AppData\Local\Temp\WinFormsCache\清空神秘文件夹 C:\Users[me]\AppData\Local\Temp\WinFormsCache\

and many other tricks I found with no result at all.以及我发现的许多其他技巧都没有结果。

The only things I haven't tried (yet) have been:我(还)没有尝试过的唯一事情是:

I haven't done those yet because the problem should be easy to fix.我还没有这样做,因为问题应该很容易解决。 Reading the message it seems that the designer lost its ability to probe for assemblies and to read for types inside an assembly it have already referenced but this is true for an inherited form only.阅读该消息,设计人员似乎失去了探测程序集和读取已引用的程序集中类型的能力,但这仅适用于继承的形式。 Crazy enough to investigate.疯狂到要调查。 Let's see if someone has already discovered the nth foolishness MS has been able to do.让我们看看是否有人已经发现了 MS 能够做的第 n 个愚蠢行为。

Check your partial classes inherit the base form as well.检查您的部分类是否也继承了基本形式。 I just created a.Net 6.0 win forms project and have these four classes working.我刚刚创建了一个.Net 6.0 win forms 项目并让这四个类正常工作。

BaseForm.designer.cs
namespace WinFormsApp1 {
    partial class BaseForm {
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing) {
            if (disposing && (components != null)) {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

    #region Windows Form Designer generated code

    private void InitializeComponent() {
        this.components = new System.ComponentModel.Container();
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(800, 450);
        this.Text = "Base Form";
    }

    #endregion
}
}

BaseForm.cs
namespace WinFormsApp1 {
public partial class BaseForm : Form {
    public BaseForm() {
        InitializeComponent();
    }
}
}

Form1.designer.cs
namespace WinFormsApp1 {
partial class Form1 : BaseForm {
    private System.ComponentModel.IContainer components = null;
    protected override void Dispose(bool disposing) {
        if (disposing && (components != null)) {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code
    private void InitializeComponent() {
        this.components = new System.ComponentModel.Container();
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(800, 450);
        this.Text = "Form1";
    }

    #endregion
}
}

Form1.cs
namespace WinFormsApp1 {
public partial class Form1 : BaseForm {
    public Form1() {
        InitializeComponent();
    }
}

} }

表 1 继承 BaseForm

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

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