C# Windows 窗体设计时窗体构造函数未调用?

发布于 2024-10-09 14:19:00 字数 120 浏览 0 评论 0原文

在设计视图中打开表单时是否不调用表单的构造函数?为什么不呢?我可以以某种方式强制调用它吗? 我通过在构造函数中显示 MessageBox 来测试它,并且仅显示表单基本类型(另一种表单)的构造函数中的 MessageBox...

Is the constructor of a form not called upon opening the form in design view? Why not? Can I somehow force it to be called?
I tested it by showing a MessageBox in the constructor, and only the MessageBox from the constructor of the form's base type (another form) is shown...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长亭外,古道边 2024-10-16 14:19:00

此行为是设计使然。 Visual Studio 中的表单设计器无法实例化正在设计的类,而是实例化其直接父类。没有办法改变这种行为。如果您需要在设计时执行某些逻辑,则必须创建一个单独的祖先来封装该逻辑并从中继承。

这也是为什么为了能够使用设计器,您不能从抽象或泛型类继承表单的原因。

This behavior is by design. The form designer in Visual Studio cannot instantiate the class being designed — instead, it instatiates its immediate parent. There is no way to change this behavior. If you need some logic to be executed during design time, you have to create a separate ancestor encapsulating that logic and inherit from it.

That's also the reason why in order to be able to use the designer you cannot inherit a form from an abstract or generic class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文