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:
.NET MAUI XAML is compiled directly into intermediate language (IL) with the XAML compiler (XAMLC). The XAML compilation provides several benefits, such as:
XAML compilation is enabled by default in .NET MAUI. The applications depending on their configuration have the following behavior:
📝 You can override the XAML compilation behavior in both configurations using 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:
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.
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
{
...
}
I hope this article about XAML compilation was beneficial to you! I encourage you to continue learning about .NET MAUI! 💚💕
See you next time! 💁♀️
This article was based on .NET MAUI’s official documentation.
Leomaris Reyes is a Software Engineer from the Dominican Republic, with more than 5 years of experience. A Xamarin Certified Mobile Developer, she is also the founder of Stemelle, an entity that works with software developers, training and mentoring with a main goal of including women in Tech. Leomaris really loves learning new things! 💚💕 You can follow her: Twitter, LinkedIn , AskXammy and Medium.