Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why XAML is compiled into BAML and not in C#

Tags:

wpf

xaml

baml

As far as I know, everything done in XAML can be done in C#.

Why XAML is compiled in BAML and not in C# ? Wouldn't be more efficient to parse the XAML at compile-time and create the corresponding C# code ?

like image 605
japf Avatar asked Sep 14 '09 20:09

japf


People also ask

How is XAML compiled?

XAML can be optionally compiled directly into intermediate language (IL) with the XAML compiler (XAMLC). XAML compilation offers a number of a benefits: It performs compile-time checking of XAML, notifying the user of any errors. It removes some of the load and instantiation time for XAML elements.

Why XAML is used in WPF?

The goal of XAML is to enable visual designers to create user interface elements directly. WPF aims to make it possible to control all visual aspects of the user interface from mark-up.

What is Baml in WPF?

BAML file from assembly resources, parses it, and creates a corresponding WPF visual tree or workflow. Having this format, the content is loadable faster during runtime, because the XAML is enriched by tokens, and lexical analysis is completed.

What is XAML in C#?

XAML is a declarative markup language typically used to create an application's user interface. It got its start in 2006 when Windows Presentation Foundation (WPF) was introduced as part of the . NET Framework 3.0. Positioned as the follow-up to Windows Forms, it's no wonder adoption was slow.


2 Answers

This blog post should provide a comprehensive answer: http://blogs.microsoft.co.il/blogs/tomershamam/archive/2007/05/25/Compiled-XAML-3D00-BAML-not-IL.aspx

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.

  1. Loose XAML file should be parsed at runtime, and can be deployed as a simple XML file, locally, remotely or embedded into the assembly.
  2. Compiled is a XAML file marked as “Page” in Visual Studio ( 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.

like image 56
petr k. Avatar answered Sep 23 '22 23:09

petr k.


The Xaml is separate from the C# code because it allows these elements to be "soft-coded." If you compile the Xaml to C# code, you defeat this characteristic, because now the UI elements, data binding, eventing, etc. are now hard-coded into the program, and you need to recompile the entire program to make a minor change to the user interface.

The blog post that petr k. references says that Xaml actually was compiled to IL at one time, but Baml is now used because:

  1. It is more secure (it cannot be executed directly), and
  2. Baml can be localized (different languages) without requiring recompilation.
like image 23
Robert Harvey Avatar answered Sep 24 '22 23:09

Robert Harvey