Compiled XAML = BAML not IL

May 25, 2007

6 comments

Yesterday I lectured about XAML and the following question was asked: Why XAML is compiled into BAML and not directly into IL for better performance?


Before giving the correct answer, I want to explain what BAML is.

There are actually two ways for handling a XAML file: Loose or Compiled.


  • Loose XAML file should be parsed at runtime, and can be deployed as a simple XML file, locally, remotely or embedded into the assembly.
  • Compiled is a XAML file marked as “Page” in Visual Studio (<Page Include=”AXamlFile.xaml” /> in MSBuild), deployed as a BAML (Binary Application Markup Language) file and embedded as an assembly resource.

A loose XAML file can’t include the x:Class XAML keyword, also it can’t embed a source code, nor it can emit code by all means. It is possible to load a loose XAML file by calling XamlReader.Load() method, casting the return value into the root element. The loose XAML version provides a dynamic way to load and change the view, but provides poor performance due to the fact that the XML file is parsed at runtime.

A compiled XAML file (BAML) can emit code, by using x:Class, or by registering events for example. It is possible to load an element from inside the BAML by calling the Application.LoadComponent(), casting the return value into the root element. The compiled XAML version provides better performance since it is pre-tokenized binary version of the XAML file, hence it is smaller and can be loaded faster, but it is not dynamic.

Once upon a time, there was CAML. CAML was the exact IL version of the compiled XAML file. Unfortunately, the WPF team has decided to eliminate it, and keep the BAML version for the following reasons:

1. BAML is compact hence it can be downloaded faster (good for XBAP applications)

2. BAML is less security threat than code execution (good for XBAP applications)

3. BAML can be localized after compilation

In the bottom line, BAML is a little slower than IL but has more advantages than CAML.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

6 comments

  1. JigneshMay 5, 2010 ב 18:01

    Good one…..Really helpful…..

    Reply
  2. Claude GlauserFebruary 24, 2011 ב 22:10

    Thanks a lot for good your explanation! It is much clearer now. One question left: Why is BAML less security thread?

    Reply
  3. Tomer ShamamFebruary 24, 2011 ב 22:54

    Hi Claude,
    It is said that BAML is safer than IL, since it can’t contain malicious code, it is only a binary stream of WPF objects, where IL can be anything.
    BAML is considered safer when dealing with XBAP applications.

    Reply
  4. LakshmiMarch 2, 2011 ב 18:33

    Your explanation saved me alot instead of wasting one more day to load loose xaml.

    Reply
  5. DavidFebruary 8, 2012 ב 18:01

    Great explanation

    Reply
  6. AlexanderAugust 21, 2013 ב 21:03

    Good explanation, thx.

    Reply