简体   繁体   English

InitializeComponent()做什么,它在WPF中如何工作?

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

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. 首先,我会特别感兴趣的是了解构造顺序的血腥细节,以及附加属性时会发生什么。

The call to InitializeComponent() (which is usually called in the default constructor of at least Window and 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()的调用InitializeComponent()通常在至少WindowUserControl的默认构造函数中调用)实际上是对控件的partial类的方法调用(而不是像我第一次预期的那样调用对象层次结构)。

This method locates a URI to the XAML for the Window / UserControl that is loading, and passes it to the System.Windows.Application.LoadComponent() static method. 此方法将URI加载到正在加载的Window / UserControl的XAML,并将其传递给System.Windows.Application.LoadComponent()静态方法。 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. LoadComponent()加载位于传入的URI中的XAML文件,并将其转换为由XAML文件的根元素指定的对象的实例。

In more detail, LoadComponent creates an instance of the XamlParser , and builds a tree of the XAML. 更详细地, LoadComponent创建XamlParser的实例,并构建XAML的树。 Each node is parsed by the XamlParser.ProcessXamlNode() . 每个节点都由XamlParser.ProcessXamlNode()解析。 This gets passed to the BamlRecordWriter class. 这将传递给BamlRecordWriter类。 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. 在此之后的一段时间,我对BAML如何转换为对象感到有点迷失,但这可能足以帮助你走上启蒙之路。

Note: Interestingly, the InitializeComponent is a method on the System.Windows.Markup.IComponentConnector interface, of which Window / UserControl implement in the partial generated class. 注意:有趣的是, InitializeComponentSystem.Windows.Markup.IComponentConnector接口上的一个方法,其中Window / UserControl在部分生成的类中实现。

Hope this helps! 希望这可以帮助!

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

The YourClass .g.cs ... is the code for generated partial class. YourClass .g.cs ...是生成的分部类的代码。 Again, if you open that up you can see the InitializeComponent method and how it calls LoadComponent ... and much more. 同样,如果你打开它,你可以看到InitializeComponent方法以及它如何调用LoadComponent ......等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM