.Net Programming Tips

1. XAML Compilation: BAML

The creators of WPF knew that XAML needed to not just solve the problem of design collaboration - it also needed to be fast. And though XML-based formats such as XAML are flexible and easily portable to other tools and platforms, they aren’t always the most efficient option. XML was designed to be logical, readable, and straightforward - not compact.

WPF addresses this shortcoming with BAML (Binary Application Markup Language). BAML is really nothing more than a binary representation of XAML. When you compile a WPF application in Visual Studio, all your XAML files are converted into BAML and that BAML is then embedded as a resource into the final DLL or EXE assembly. BAML is tokenized, which means lengthier bits of XAML are replaced with shorter tokens. Not only is BAML significantly smaller, it’s also optimized in a way that makes it faster to parse at runtime.

ref : http://en.csharp-online.net/XAML—XAML_Compilation

2. WPF and XAML

XAML and WPF are separate, albeit complementary, technologies. As a result, it’s quite possible to create a WPF application that doesn’t use the faintest bit of XAML.

Altogether, there are three distinct coding styles that you can use to create a WPF application:

  1. Code-only - This is the traditional approach used in Visual Studio for Windows Forms applications. It generates a user interface through code statements.

  1. Code and uncompiled markup (XAML) - This is a specialized approach that makes sense in certain scenarios where you need highly dynamic user interfaces. You load part of the user interface from a XAML file at runtime using the XamlReader class from the System.Windows.Markup namespace.

  1. Code and compiled markup (BAML) - This is the preferred approach for WPF, and the one that Visual Studio supports. You create a XAML template for each window and this XAML is compiled into BAML and embedded in the final assembly. At runtime the compiled BAML is extracted and used to regenerate the user interface.

ref: http://en.csharp-online.net/XAML—Loading_and_Compiling_XAML