[英]Form inheritance crashes with .NET 6
我正在将旧的 winforms 应用程序从 4.8 转换为 6.0.1。 转换非常简单,应用程序完全没有问题,直到突然,Visual Studio(2022 64 位 v17.0.5)拒绝向表单设计器显示从另一个表单继承的每个表单。
我缩小了我的项目,直到我发现它不是我......
using System.Windows.Forms;
namespace MyApp
{
public partial class BaseForm : Form
{
public BaseForm()
{
InitializeComponent();
}
}
}
和
namespace MyApp
{
public partial class ChildForm : BaseForm
{
public ChildForm()
{
InitializeComponent();
}
}
}
每个 class 的其他部分(winforms 将表格 class 拆分为两个部分)完全不变。
两个 forms 完全是空的,但设计者拒绝绘制 ChildForm 返回这个漂亮而无意义的消息:
无法为此文件显示设计器,因为无法设计其中的任何类。 设计器检查了文件中的以下类: 无法为该文件显示设计器,因为其中没有任何类可以设计。 设计器检查了文件中的以下类:\r\n ChildForm --- 无法加载基础 class 'MyApp.BaseForm'。 确保已引用程序集并且已构建所有项目。
我已经尝试了在 Internet 上找到的所有内容:
:base()
指令以及我发现的许多其他技巧都没有结果。
我(还)没有尝试过的唯一事情是:
我还没有这样做,因为问题应该很容易解决。 阅读该消息,设计人员似乎失去了探测程序集和读取已引用的程序集中类型的能力,但这仅适用于继承的形式。 疯狂到要调查。 让我们看看是否有人已经发现了 MS 能够做的第 n 个愚蠢行为。
检查您的部分类是否也继承了基本形式。 我刚刚创建了一个.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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.