Winforms Designer如何实例化我的表单? [英] How does the Winforms Designer instantiate my form?

查看:114
本文介绍了Winforms Designer如何实例化我的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发自己的WinForms设计器。它必须能够加载现有的自定义表单类型。我遇到的问题之一是没有默认ctor的表单:我的代码当前实例化了表单,然后才能将其加载到设计器中,这需要默认的ctor。

I'm developing my own WinForms designer. It must be able to load existing custom form types. One of the issues I hit is forms without a default ctor: My code currently instantiates the form before it can load it into the designer, which requires a default ctor.

OTOH ,VS2008能够加载此类表格。我相信它实际上不会实例化我的表单(如此问题中所述):即使默认的ctor也不会执行。它并没有真正执行InitializeComponent()。我只是在该函数中添加了一个消息框,但它没有显示。

OTOH, VS2008 is able to load such forms. I believe it doesn't actually instantiate my form (as noted in this question): Even default ctors are not executed. And it doesn't truly execute InitializeComponent(). I just added a messagebox in that function and it doesn't show.

它看起来像是在动态模拟自定义表单类型,并且只执行InitializeComponent中的部分代码。

It looks like it dynamically mimic the custom form type and executes only parts of the code in InitializeComponent which it thinks is relevant.

有人知道我在哪里可以找到有关VS设计器工作方式的更多信息。

Does anyone know where I can find more information regarding how the VS designer works.

TIA。

注意:我发现了这个相关问题,但没有满意的答案

Note: I found this related question without satisfying answers

编辑:附加信息:Steve将我指向CodeDom,这非常有趣。我的问题是,我需要加载到设计器中的类型已经编译,而不是作为源代码使用。我找不到将CodeDom反序列化应用于已编译代码的任何方法。

Additional info: Steve points me to CodeDom, which is very insteresting. My problem though is that the types I need to load into my designer are already compiled instead of being available as source code. I can't find any way to apply CodeDom deserialization to compiled code.

推荐答案

找到了这个此处


在VS中打开新的Windows
应用程序项目时,在设计
视图中会看到一个名为Form1的
空表单。现在,您尚未构建
项目,那么设计师
如何创建Form1
的实例并进行显示?好吧,设计师根本没有真正实例化Form1。
正在创建Form1的基本
类的实例,即
System.Windows.Forms.Form。有了面向对象
编程的
基础知识,您会发现,此
直观地有意义。在
设计Form1时,您将从
基类Form入手,并对其进行自定义。
这正是设计师
可以帮助您完成的任务。

When you open a new Windows Application project in VS, you see an empty form called Form1 in design view. Now, you haven't built the project yet, so how is the designer able to create an instance of Form1 and show it? Well, the designer is not really instantiating Form1 at all. It is creating an instance of the base class of Form1, i.e., System.Windows.Forms.Form. With a basic knowledge of object oriented programming, you will find that this intuitively makes sense. When you are designing Form1, you start with the base class, Form, and customize it. This is exactly what the designer helps you to do.

现在,假设您向窗体添加了一些
控件并关闭了
设计师。当您重新打开
设计器时,控件仍然在
那里。但是,基类Form
上没有这些控件,因此
如果设计者未运行Form1的
构造函数,它如何显示
控件?设计人员通过反序列化
InitializeComponent中的代码来完成
的工作。设计师支持的每种语言
都有一个
CodeDomProvider,负责提供
来提供用于解析InitializeComponent中
代码的解析器,而
创建一个$ b的CodeDom表示形式$ b。然后,设计人员调用一组
CodeDomSerializer,以将该
反序列化为实际控件(或更广泛地说是
组件),然后可以将其添加到
设计时窗体中。现在,我在
描述中的很多细节上都用了
,但是这里要说明的是
,Form1的构造函数和
InitializeComponent从来没有真正被调用过。而是,设计器解析
InitializeComponent
中的语句,以找出要实例化
并将其添加到表单中的控件。

Now let's say you added a bunch of controls to the Form and closed the designer. When you reopen the designer, the controls are still there. However, the base class Form doesn't have these controls on it, so if the designer isn't running the constructor of Form1, how did it show the controls? The designer does this by deserializing the code in InitializeComponent. Each language that the designer supports has a CodeDomProvider that is responsible for providing a parser that parses the code in InitializeComponent and creates a CodeDom representation of it. The designer then invokes a set of CodeDomSerializers to deserialize this into actual Controls (or more broadly, Components) that it can add to the design time Form. Now, I have glossed over a lot of details in that description, but the point here is that Form1's constructor and InitializeComponent are never really invoked. Instead, the designer parses the statements in InitializeComponent to figure out what controls to instantiate and add to the form.






以上是Visual Studio中Windows Forms设计器加载表单的方式。如果您要寻找的是创建没有默认构造函数 且仍然可以访问其中包含的组件/控件的表单实例的方法,则我不知道解决方案。我知道的唯一允许您绕过缺少默认构造函数的方法是 FormatterServices.GetUninitializedObject ,但请注意...


The above is how Windows Forms designer in Visual Studio loads a form. If what you are looking for is a way to create an instance of a form that has no default constructor and still have access to the contained components/controls, I'm not aware of a solution. The only method I'm aware of that allows you to bypass the lack of a default constructor is FormatterServices.GetUninitializedObject, but beware ...


将对象
的新实例初始化为零,并且没有运行
构造函数,则对象
可能不代表该对象认为
有效的状态。

Because the new instance of the object is initialized to zero and no constructors are run, the object might not represent a state that is regarded as valid by that object.

我也有一个需要实例化已编译表单的应用程序,但始终使用 Activator.CreateInstance ,并要求其他开发人员至少包括私有的默认构造函数(如果他们希望在Windows中访问其表单)我的应用由于我们拥有整个代码库,并且每个人都知道要求,所以这不是问题,对我们来说很好。

I too have an app that requires instantiating compiled forms but have always used Activator.CreateInstance and required other developers to include, at the very least, a private default constructor if they want their form accessible in my app. Since we own the entire codebase and everyone is aware of the requirement, this isn't a problem and works out well for us.

这篇关于Winforms Designer如何实例化我的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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