I would like to understand the compilation process of XAML files. Sorry for putting this question here but I really did not find any resource explaining this process in-depth.
I understand that XAML is compiled into a .baml file. But: Is the .baml compiled from the generated .g.cs file? Or is the .baml independent and is the IL code generated from the generated .g.cs AND the original .xaml.cs file - this would explain why MainWindow is partial. Which parts of the XAML declarations go into the BAML file? I would like to know also when the loading of a .baml file takes place (for example when talking about a window). Thanks for the help.
. NET Multi-platform App UI (. NET MAUI) XAML is 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. It helps to reduce the file size of the final assembly by no longer including . xaml files.
Step 1: The first step is to compile the XAML files into BAML using the xamlc.exe compiler. For example, if our project includes a file name Window1. xaml, the compiler will create a temporary file named Window1.
To debug workflow XAMLRight-click the . xaml file that contains your workflow definition and select View Code. You will see a breakpoint displayed on the same line as the XAML element declaration of the activity that you set the breakpoint on in the design view.
In my understanding based on the reference below, everything declared in XAML gets compiled to BAML; .g.cs
and .xaml.cs
files compiled to IL; .xaml.cs
IL generated from codes in .xaml.cs
file (obviously), and g.cs
IL contains codes generated to interact with BAML (instead of IL codes generated from BAML it self).
Check this blog post for reference. To summarize, the author said that compilation of XAML happened in 2 steps :
Step 1. The first step is to compile the XAML files into BAML using the xamlc.exe compiler. For example, if our project includes a file name Window1.xaml, the compiler will create a temporary file named Window1.baml and place it in the obj\Debug subfolder (in our project folder). At the same time, a partial class is created for our window, using the language of our choice. For example, if we’re using C#, the compiler will create a file named Window1.g.cs in the obj\Debug folder. The g stands for generated.
The partial class includes three things:
• Fields for all the controls in our window.
• Code that loads the BAML from the assembly, thereby creating the tree of objects. This happens when the constructor calls Initialize Component ().
• Code that assigns the appropriate control object to each field and connects all the event handlers. This happens in a method named Connect (), which the BAML parser calls every time it finds a named object.
Step 2. When the XAML-to-BAML compilation stage is finished, Visual Studio uses the appropriate language compiler to compile our code and the generated partial class files. In the case of a C# application, it’s the csc.exe compiler that handles this task. The compiled code becomes a single assembly Window1.exe) and the BAML for each window is embedded as a separate resource.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With