0

我正在将旧的 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();
        }
    }
}

每个类的其他部分(winforms 将表单类分成两部分)完全保持原样。

这两个表单是完全空的,但设计者拒绝绘制 ChildForm 返回这个漂亮而无意义的信息:

无法为此文件显示设计器,因为无法设计其中的任何类。设计器检查了文件中的以下类: 无法为该文件显示设计器,因为其中没有任何类可以设计。设计器检查了文件中的以下类: \r\n ChildForm --- 无法加载基类“MyApp.BaseForm”。确保已引用程序集并且已构建所有项目。 在此处输入图像描述

我已经尝试了在 Internet 上找到的所有内容:

  • 清理并重建解决方案
  • 退出 VS
  • :base()在两种形式的 ctor 上放置一个明确的(且无用的)指令
  • 清空神秘文件夹 C:\Users[me]\AppData\Local\Temp\WinFormsCache\

以及我发现的许多其他技巧都没有结果。

我(还)没有尝试过的唯一事情是:

我还没有这样做,因为问题应该很容易解决。阅读该消息,设计人员似乎失去了探测程序集和读取已引用的程序集中类型的能力,但这仅适用于继承的形式。疯狂到要调查。让我们看看是否有人已经发现了 MS 能够做的第 n 个愚蠢行为。

4

1 回答 1

1

检查您的部分类是否也继承了基本形式。我刚刚创建了一个 .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

于 2022-01-19T17:47:43.503 回答