Professional, Software

InitializeFromXaml Vs XamlReader.Load

When you create a new user control item in VS, it generates the following line of code. This API as I have said in this post, creates an object tree from the Xaml passed in string format.

_progressBarRoot = InitializeFromXaml(_progressBarLook) as Canvas;



Silverlight also has an API called XamlReader.Load which also creates object tree from a XAML that is passed in string format. So why these two methods. XamlReader.Load also exists in different form in WPF. The one is Silverlight Alpha 1.1 is essentially managed equivalent of the CreateFromXaml API exposed for JavaScript users. so why to use IntializeFromXaml when you have XamlReader.Load.

This is essentially to make things easy for control developers. There are few things that need to happen when you are creating a object tree from XAML in string format in the custom control scenario…

  1. Create object tree that represents the XAML in string format
  2. object tree need to be created with namescoping turned on so that you can instantiate same control multiple times on a given page and names of individual element would not conflict
  3. Three tree that is created should become child of Control class
  4. Event handlers that are specified in the XAML should be hooked. Parser should look for those event handler in the class instead of root element of the XAML as it typically does for the page XAML.
<Canvas xmlns="" 

then ‘_rootCanvas_MouseEnter is implemented on custom class that inherits from Control class rather than Canvas at the root.

If we were to use the XamlReader.Load instead of InitializeFromXaml then…

  1. You would need to inherit from Canvas
  2. you will need to call XamlReader.Load (string, bool) with namescoping turned on
  3. You need to add the tree returned by XamlReader to Canvas.Children collection
  4. Hook up all the event handler in code using language specific syntax.

#1 is not the correct model because Canvas has multiple properties that Control class does not need. E.g. it has children property because it is designed to be a container for various element. Control, should have it a single child that holds the visual look of the control, Canvas has Top, Left dependency properties or background properties that are needed on Control. Once you decide to use Control as base class now you can not do #3 because Control does not Children property (or Child!). IntializeFromXaml automatically takes care of these things for you by adding the  tree under the control internally.


3 thoughts on “InitializeFromXaml Vs XamlReader.Load

  1. Pingback: Silverlight Cream for September 24, 2007

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s