这是什么的InitializeComponent()做的,以及它是如何在WPF的工作?它是、这是什么、如何在、工作

2023-09-02 01:21:04 作者:久了就旧

这是什么的InitializeComponent()做的,它是如何在WPF的工作?

What does InitializeComponent() do, and how does it work in 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.

推荐答案

的InitializeComponent()(这通常被称为至少在窗口和用户控件)实际上是一个方法调用的控制(而不是调用该对象的层次结构的部分类我第一次预期)。

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).

此方法查找的URI XAML的窗口 / 用户控件正在加载,并将其传递到在 System.Windows.Application.LoadComponent()静态方法。 LoadComponent()载入位于传递URI中的XAML文件,并将其转换为是由XAML文件的根元素指定的对象的实例。

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. 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 创建XAML的 XamlParser ,并建立一棵树的一个实例。每个节点由 XamlParser.ProcessXamlNode解析()。这被传递到 BamlRecordWriter 类。过了一段时间后,这我得到一点在BAML如何转换为对象,失去了,但是这可能足以帮助你的道路上的启蒙。

In more detail, LoadComponent creates 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 BamlRecordWriter class. 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.

注:有趣的是,对的InitializeComponent System.Windows.Markup.IComponentConnector 接口的方法,其中窗口 / 用户控件在部分生成的类实现。

Note: Interestingly, the InitializeComponent is a method on the System.Windows.Markup.IComponentConnector interface, of which Window/UserControl implement in the partial generated class.

希望这有助于!