InitializeComponent() 有什么作用,它在 WPF 中是如何工作的? [英] What does InitializeComponent() do, and how does it work in WPF?
问题描述
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()
的调用(通常在至少 Window
的默认构造函数中调用和 UserControl
) 实际上是对控件部分类的方法调用(而不是像我最初预期的那样调用对象层次结构).
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).
此方法为正在加载的 Window
/UserControl
定位 XAML 的 URI,并将其传递给 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
创建 XamlParser
的一个实例,并构建 XAML 树.每个节点都由 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
接口上的一个方法,其中Window
/UserControl
在部分生成的类中实现.
Note: Interestingly, the InitializeComponent
is a method on the System.Windows.Markup.IComponentConnector
interface, of which Window
/UserControl
implement in the partial generated class.
希望这有帮助!
这篇关于InitializeComponent() 有什么作用,它在 WPF 中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!