C# InitializeComponent() 的非常简单的定义;方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12297079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Very Simple definition of InitializeComponent(); Method
提问by JsonStatham
I have been working through the Head First C# book and have used the InitializeComponent(); method several times.
我一直在研究 Head First C# 书,并使用了 InitializeComponent(); 方法多次。
An example of this is on the Party Planner exercise I created a class called DinnerParty.cs and then used this code in the Form1.cs
这方面的一个例子是在派对策划者练习中我创建了一个名为 DinnerParty.cs 的类,然后在 Form1.cs 中使用了此代码
public Form1()
{
InitializeComponent();
dinnerParty = new DinnerParty() { NumberOfPeople = 5 };
dinnerParty.SetHealthyOption(checkBox2.Checked);
dinnerParty.CalculateCostOfDecorations(checkBox1.Checked);
DisplayDinnerPartyCost();
}
My Question is, what exactly is the Initialize Component method doing. My understanding is that I am defining a new object or instance of the DinnerParty class and setting up all the values, so far I have assumed that InitializeComponent() is kind of saying "Set up values of my fields using the following:"
我的问题是,Initialize Component 方法到底在做什么。我的理解是,我正在定义 DinnerParty 类的一个新对象或实例并设置所有值,到目前为止,我假设 InitializeComponent() 有点说“使用以下内容设置我的字段的值:”
Could I please have a BASIC, something I can get my head around definition. I have looked at previous posts and answers regarding this and everything is too complex. I will mark the easiest to understand response that still has the key information as the answer.
我可以请一个基本的,我可以理解定义的东西。我看过以前的帖子和关于这个的答案,一切都太复杂了。我将标记仍然包含关键信息的最容易理解的响应作为答案。
采纳答案by Steve
InitializeComponentis a method automatically written for you by the Form Designer when you create/change your forms.
InitializeComponent是一种在您创建/更改表单时由表单设计器自动为您编写的方法。
Every Forms file (e.g. Form1.cs) has a designer file (e.g. Form1.designer.cs) that contains the InitializeComponent method, the override of the generic Form.Dispose, and the declaration of all of your User Interface objects like buttons, textboxes, labels and the Form itself.
每个表单文件(例如 Form1.cs)都有一个设计器文件(例如 Form1.designer.cs),其中包含 InitializeComponent 方法、通用Form.Dispose的覆盖以及所有用户界面对象(如按钮、文本框)的声明、标签和表单本身。
The InitializeComponentmethod contains the code that creates and initializes the user interface objects dragged on the form surface with the values provided by you (the programmer) using the Property Grid of the Form Designer. Due to this fact do not ever try to interact with the form or the controls before the call to InitializeComponent.
Also, you will find here, the plumbing required to link the controls and form events to the specific event handlers you have written to respond to the user actions.
所述的InitializeComponent方法包含创建并初始化使用窗体设计的属性网格的用户接口对象与由您(程序员)提供的值的形式表面上拖动的代码。由于这个事实,在调用 InitializeComponent 之前不要尝试与表单或控件交互。
此外,您将在此处找到将控件和表单事件链接到您为响应用户操作而编写的特定事件处理程序所需的管道。
The code contained in Form1.cs and the Form1.Designer.cs files is part of the same class thanks to the concept of partial classesthat could keep two or more files of your code together like a single block of code.
Form1.cs 和 Form1.Designer.cs 文件中包含的代码是同一个类的一部分,这要归功于部分类的概念,它可以像单个代码块一样将代码的两个或多个文件保存在一起。
Of course, due to the high numbers of changes executed by the Form Designer, it is a really good advice to not try to modify manually this method, while, sometime, I find useful to add code to the Dispose method with the purpose to destroy some unmanaged objects created in the form lifetime.
当然,由于表单设计器执行了大量更改,因此不要尝试手动修改此方法是一个非常好的建议,而有时,我发现将代码添加到 Dispose 方法中以破坏目的很有用在表单生命周期中创建的一些非托管对象。
回答by NeilD
InitializeComponent is a method which is used to initialize your form. It has nothing to do with your DinnerParty class.
InitializeComponent 是一种用于初始化表单的方法。它与您的 DinnerParty 课程无关。
So, it might be setting up things like the buttons, labels, event handlers, and so on on your User Interface.
因此,它可能会在您的用户界面上设置按钮、标签、事件处理程序等内容。
Here's a more in depth explanation. What does InitializeComponent() do, and how does it work in WPF?

