#101 – What Visual Studio Does with Your XAML

Given that XAML is just a declarative representation of the objects used by your WPF application, the objects need to be instantiated at runtime.

Here’s the process (e.g. for MainWindow.xaml):

  • While you work in designer, VS2010 automatically generates partial class (e.g. MainWindow.g.i.cs), including:
    • Reference variables for named elements in XAML  (e.g. myButton)
    • Code to instantiate XAML objects at runtime
  • When you build the project
    • All code (yours and VS2010-generated) is compiled
    • XAML is compiled into BAML (binary), stored in .baml file
    • All BAML files combined into single .g.resources file (e.g. MyApp.g.resources)
    • Executable is built, embedding the .g.resources file as a resource
  • At runtime
    • Window constructor calls InitializeComponent
    • InitializeComponent (in generated code) calls Application.LoadComponent, passing URI identifying XAML
    • LoadComponent loads binary XAML from embedded resource, creates all objects
    • As BAML is read, window’s IComponentConnector.Connect method is called, which hooks up local object references to the created objects and attaches event handlers
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #101 – What Visual Studio Does with Your XAML

  1. >◾XAML is compiled into BAML (binary), stored in .baml file
    I would not call that process “compilation”. As far as I know it is just binary serialization.
    The compiled XAML was called CAML and was really compiled to the IL code. Microsoft decided that BAML was fast enough and CAML was forgotten. And Silverlight doesn’t even use BAML.

    • Sean says:

      You’re right in saying that the process is not compilation in the sense that we’re not generating machine code, or even IL (which is not machine code in any case). So it just depends on your definition of the term “compilation”. If you mean “converting from source code into binary machine code”, the the process is not compilation, but neither is the process of converting your C# to IL, since IL is not hardware-specific machine code. But in the sense that both produce binary code that is then consumed at runtime, both process (C# to IL and XAML to BAML) are typically referred to as compilation in the community. Really just a matter of convention. See http://robrelyea.wordpress.com/2007/06/14/compile-xaml-to-c-cs/ (Rob is on the XAML team) and http://msdn.microsoft.com/en-us/library/ms788718.aspx#Localize_a_WPF_Application (MSDN article that mentions BAML as the “compiled” form of XAML).

  2. GrimDazzle says:

    So, what happens to loose Xaml? If there is no InitializeComponent method for loose Xaml as there is no code behind, can we never create it’s instance. Or what’s its real use?

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: