Speed up your app’s loading time with these XAML compilation best practices in .NET MAUI.
As an app developer, you know that every second counts when it comes to loading times. That’s why we’re excited to share with you a technique that can help speed things up! In this article, we’ll be discussing how to compile XAML views and the benefits it can bring.
I’ll divide this article into the following sections:
- Learning about XAMLC
- Exploring the XamlCompilationAttribute class
- Enabling the compilation
- Disabling the compilation
Learning About XAMLC
.NET MAUI XAML is compiled directly into intermediate language (IL) with the XAML compiler (XAMLC). The XAML compilation provides several benefits, such as:
- Providing error notifications by performing compile-time checking of XAML
- Saving time by reducing instantiation and loading times
- Reducing the size of the final assembly by not including more .xaml files.
XAML compilation is enabled by default in .NET MAUI. The applications depending on their configuration have the following behavior:
- Applications built with a debug configuration receive XAML compile-time validation. This feature includes the XAML files as embedded resources in the app package, rather than converting them to IL in the assembly. The XAML files are then evaluated at runtime.
- Applications built with the release configuration receive XAML compile-time validation, as well as the conversion of the XAML to IL that is written to the assembly.
You can override the XAML compilation behavior in both configurations using the
XamlCompilationAttribute
class.
Exploring the XamlCompilationAttribute Class
The XamlCompilationAttribute
class provides control over whether XAML is compiled at compile time or at runtime. You can pass a value of XamlCompilationOptions
, which is an enum containing
values that control when XAML is compiled in IL. This enumeration supports a bitwise combination of its member values.
The XamlCompilationOptions
enum includes the following values:
- Compile: Choose this option to compile the XAML for the class or project during application build.
- Skip: Skipping compilation for this type or assembly is the task’s responsibility. The XAML will be parsed and the object graph constructed at runtime.
Enabling the Compilation
To enable XAML compilation, you need to pass XamlCompilationOptions.Compile
to the XamlCompilationAttribute
. While you can use the attribute anywhere, it’s recommended to add it to
the MauiProgram.cs file as shown here:
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
Enabling XAML compilation at the type level is also possible:
[XamlCompilation (XamlCompilationOptions.Compile)]
public partial class DetailPage : ContentPage
{
...
}
Also, by enabling compiled bindings, you can improve data binding performance in .NET MAUI applications. Compiled bindings resolve binding expressions at compile time and integrate with IntelliSense, resulting in faster binding resolution than classic bindings.
For more details about improving app performance using compiled bindings, check out this article.
Disabling the Compilation
At runtime, XAML is parsed and interpreted. It’s worth noting that, while you have the option to disable XAML compilation in your application, it’s not recommended; it will negatively impact your application’s performance.
[assembly: XamlCompilation(XamlCompilationOptions.Skip)]
Enabling XAML compilation at the type level is also possible, as shown here:
[XamlCompilation (XamlCompilationOptions.Skip)]
public partial class DetailPage : ContentPage
{
...
}
Conclusion
I hope this article about XAML compilation was beneficial to you! I encourage you to continue learning about .NET MAUI!
See you next time! ♀️
References
This article was based on .NET MAUI’s official documentation.