C# InitializeComponent() 有什么作用,它在 WPF 中是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/245825/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:40:54  来源:igfitidea点击:

What does InitializeComponent() do, and how does it work in WPF?

c#.netwpfattached-properties

提问by Tim Lovell-Smith

What does InitializeComponent()do, and how does it work in WPF?

它有什么作用InitializeComponent(),它在 WPF 中是如何工作的?

In general first, but I would especially be interested to know the gory details of order of construction, and what happens when there are Attached Properties.

一般来说,首先,但我特别想知道构造顺序的血腥细节,以及当有附加属性时会发生什么。

采纳答案by Brad Leach

The call to InitializeComponent()(which is usually called in the default constructor of at least Windowand UserControl) is actually a method call to the partial class of the control (rather than a call up the object hierarchy as I first expected).

调用InitializeComponent()(通常在至少Windowand的默认构造函数中UserControl调用)实际上是对控件的部分类的方法调用(而不是我最初预期的调用对象层次结构)。

This method locates a URI to the XAML for the Window/UserControlthat is loading, and passes it to the System.Windows.Application.LoadComponent()static method. LoadComponent()loads the XAML file that is located at the passed in URI, and converts it to an instance of the object that is specified by the root element of the XAML file.

此方法为正在加载的Window/定位 XAML 的 URI UserControl,并将其传递给System.Windows.Application.LoadComponent()静态方法。LoadComponent()加载位于传入的 URI 处的 XAML 文件,并将其转换为由 XAML 文件的根元素指定的对象的实例。

In more detail, LoadComponentcreates an instance of the XamlParser, and builds a tree of the XAML. Each node is parsed by the XamlParser.ProcessXamlNode(). This gets passed to the BamlRecordWriterclass. Some time after this I get a bit lost in how the BAML is converted to objects, but this may be enough to help you on the path to enlightenment.

更详细地说,LoadComponent创建 的实例XamlParser,并构建 XAML 的树。每个节点都由XamlParser.ProcessXamlNode(). 这将传递给BamlRecordWriter类。在此之后的一段时间,我对如何将 BAML 转换为对象感到有些迷茫,但这可能足以帮助您走上启蒙之路。

Note: Interestingly, the InitializeComponentis a method on the System.Windows.Markup.IComponentConnectorinterface, of which Window/UserControlimplement in the partial generated class.

注意:有趣的InitializeComponent是,是System.Windows.Markup.IComponentConnector接口上的一个方法,其中Window/UserControl在部分生成的类中实现。

Hope this helps!

希望这可以帮助!

回答by cplotts

Looking at the code always helps too. That is, you can actually take a look at the generated partial class (that calls LoadComponent) by doing the following:

查看代码也总是有帮助的。也就是说,您实际上可以通过执行以下操作来查看生成的分部类(调用LoadComponent):

  1. Go to the Solution Explorer pane in the Visual Studio solution that you are interested in.
  2. There is a button in the tool bar of the Solution Explorer titled 'Show All Files'. Toggle that button.
  3. Now, expand the objfolder and then the Debugor Releasefolder (or whatever configuration you are building) and you will see a file titled YourClass.g.cs.
  1. 转到您感兴趣的 Visual Studio 解决方案中的解决方案资源管理器窗格。
  2. 解决方案资源管理器的工具栏中有一个名为“显示所有文件”的按钮。切换那个按钮。
  3. 现在,展开obj文件夹,然后展开DebugRelease文件夹(或您正在构建的任何配置),您将看到一个名为YourClass.g.cs的文件。

The YourClass.g.cs ... is the code for generated partial class. Again, if you open that up you can see the InitializeComponent method and how it calls LoadComponent ... and much more.

YourClass.g.cs ...是生成的部分类的代码。同样,如果你打开它,你可以看到 InitializeComponent 方法以及它如何调用 LoadComponent ......等等。