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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The call to
InitializeComponent()(which is usually called in the default constructor of at leastWindowandUserControl) is actually a method call to the partial class of the control (rather than a call up the object hierarchy as I first expected).This method locates a URI to the XAML for the
Window/UserControlthat is loading, and passes it to theSystem.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.In more detail,
LoadComponentcreates an instance of theXamlParser, and builds a tree of the XAML. Each node is parsed by theXamlParser.ProcessXamlNode(). This gets passed to theBamlRecordWriterclass. 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.Note: Interestingly, the
InitializeComponentis a method on theSystem.Windows.Markup.IComponentConnectorinterface, of whichWindow/UserControlimplement in the partial generated class.