C#Windows窗体设计时窗体构造函数未调用? [英] C# Windows Forms Design-time Form constructor not called?

查看:253
本文介绍了C#Windows窗体设计时窗体构造函数未调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计视图中打开表单时,不调用窗体的构造函数?为什么不?我可以不知何故强迫它被叫?
我通过在构造函数中显示一个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...

推荐答案

这种行为是设计的。 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.

这篇关于C#Windows窗体设计时窗体构造函数未调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆