168

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.

Danny Beckett
  • 18,294
  • 21
  • 100
  • 129
Tim Lovell-Smith
  • 13,077
  • 11
  • 67
  • 89
  • 2
    Thanks, I think these are pretty good answers below! Nobody mentioned the AttachedProperties exactly, but now I know that any AttachedProperties in Xaml are just created as part of the Xaml parsing, so they don't really deserve a special mention. – Tim Lovell-Smith Oct 21 '09 at 17:13

2 Answers2

160

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

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.

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.

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

Hope this helps!

Danny Beckett
  • 18,294
  • 21
  • 100
  • 129
Brad Leach
  • 16,367
  • 17
  • 68
  • 86
  • @Brad, how did you find which interface InitializeComponent is defined in? F1 help on the call in the .xaml.cs file leads leads to "page not found" while in .g.cs or .g.i.cs file leads to the Microsoft.SPOT.Emulator.EmulatorComponent class. I'm new to WPF. Is this method generated at build time? – Vimes Apr 19 '13 at 23:37
  • @АртёмЦарионов Without a call to InitializeComponent in the constructor, the control will not display or be usable in the XAML in which it is located. – Jason Rae Oct 03 '13 at 14:37
  • Interesting. I was under the impression that the xaml was only used during compilation.. What point is it to have the xaml available at runtime and where is it stored? – Jesper Matthiesen Nov 24 '15 at 14:26
  • Why do some methods give me a "object reference is not set on an instance of an object." ? – Peter Gruppelaar Nov 23 '17 at 15:47
  • @JesperMatthiesen Maybe for hot reloading? So that during debugging changes to XAML get reflected during runtime without having to restart / recompile the application. – Michael Schnerring Aug 25 '20 at 11:50
26

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:

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

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.

cplotts
  • 13,443
  • 8
  • 53
  • 63