XAML Compilation in Xamarin.Forms
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.
- It helps to reduce the file size of the final assembly by no longer including .xaml files.
XAML compilation is disabled by default in the framework. However, it's enabled in the templates for new projects. It can be explicitly enabled or disabled (
XamlCompilationOptions.Skip) at both the assembly and class level by adding the
The following code example demonstrates enabling XAML compilation at the assembly level:
[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
While the attribute can be placed anywhere, a good place to put it is in AssemblyInfo.cs.
In this example, compile-time checking of all the XAML contained within the assembly will be performed, with XAML errors being reported at compile-time rather than run-time. Therefore, the
assembly prefix to the
XamlCompilation attribute specifies that the attribute applies to the entire assembly.
The following code example demonstrates enabling XAML compilation at the class level:
public class HomePage : ContentPage
In this example, compile-time checking of the XAML for the
HomePage class will be performed and errors reported as part of the compilation process.
Compiled bindings can be enabled to improve data binding performance in Xamarin.Forms applications. For more information, see Compiled Bindings.