WPF XAML and BAML: XAML Introduction, Advantages and Compilation
WPF XAML and BAML: XAML Introduction, Advantages and Compilation
This article on WPF XAML covers basic concepts of XAML, Advantages of using XAML into WPF, how XAML elements are related with .NET classes and how XAML in actually compiled in WPF applications. We will get an idea on how WPF XAML code is converted into the BAML and again how BAML code is converted into the XAML and the role of InitializeComponent() method in the process.
XAML stands for Extensible Application Markup Language and is mainly used to create WPF user interface. XAML is also used in Silverlight and WF (Windows Workflow Foundation). You can create XAML using Visual Studio as well as Expression Blend. Basically Visual Studio is for developers and Expression Blend is for designers.
Advantage of XAML in WPF applications
In WPF, due to the introduction of XAML, graphical designer can separately work on user interface and developers can concentrate on the main logic. But it was not possible in Windows Form because any visual element such as label, textbox, button you create in Windows Forms are defined in C# classes. So, the designer could not do anything in that. All the user interface thing had to be taken care by developer itself. Designers could make separate mock-ups for screens in Photoshop etc. which developers had to convert in the C# code which was very much painful. But WPF XAML changed the world. Now designers can directly make screens for developers in XAML and developers just have to bind the elements in the screen with the properties they have made in their C# code.
Relation between XAML Elements and .NET Classes
Every element in XAML document maps to an instance of .NET class. The name of the element matches to the name of the .NET class. For example, the element Button in XAML instructs WPF to create an object of Button class in .NET. XAML element attributes are used to set the properties of .NET classes.
Note: XAML is case-sensitive language.
For example: You cannot write below XAML code
like this
Conversion of XAML into BAML: XAML Compilation
When you compile your WPF application, all your XAML code is converted into the BAML (Binary Application Markup Language) by XAML parser and is embedded as a resource into the final DLL or EXE. BAML is nothing but the binary representation of XAML. BAML is tokenized, which means lengthier bits of XAML are replaced with shorter tokens. Not only BAML is significantly smaller, but it is also optimized in a way that makes it faster to parse at runtime.
Conversion of BAML into XAML: The InitializeComponent() Method
InitializeComponent() method plays a vital role in WPF applications. Therefore, you should never delete InitializeComponent() call in your window's constructor. Similarly, if you add another constructor to your window class, make sure it also calls InitializeComponent().
InitializeComponent() calls LoadComponent() method of the System.Windows.Application class. The LoadComponent() method extracts the BAML(the compiled XAML) from your assembly and uses it to build your WPF user interface.