Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  

How to load BAML resources

What is BAML?

WPF is based on XAML an XML dialect to describe object graphs. This is very powerful but parsing an XAML file at runtime is quite expensive. So the MarkupCompiler converts the XAML file to a more compact binary version called BAML. The BAML stream then is stored to the resources of the assembly and loaded within the InitializeComponent method.

The BAML API is something unique of WPF and was not public until .NET 4.0. Now it's available through the Baml2006Reader implementation. The following code shows how to load a BAML stream in .NET 3.5 and 4.0

Load an object from a BAML stream

Load a BAML stream in .NET 3.5 (by using reflection):

var pc = new ParserContext();
var readerType = presentationFrameworkAssembly
var method = readerType.GetMethod("LoadBaml", 
                  BindingFlags.NonPublic | BindingFlags.Static);
return method.Invoke(null, new object[] {stream, pc, null, false});

Load a BAML stream in .NET 4.0:

var reader = new Baml2006Reader(stream);
var writer = new XamlObjectWriter(reader.SchemaContext);
return writer.Result;

Last modified: 2011-03-14 20:49:21
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Commented on 20.April 2011
It is very helpful !!! Thanks

E-Mail (optional)