XAML Compilation

Using the XAML compiler to increase the performance of a Xamarin.Forms app

PDF for offline use:
Related APIs:

Let us know how you feel about this.


0/250
Thanks for the feedback!

last updated: 2016-01

XAML can be optionally compiled directly into intermediate language (IL) with the XAML compiler (XAMLC).

XAMLC 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.

XAMLC is disabled by default to ensure backwards compatibility. It can be enabled at both the assembly and class level by adding the XamlCompilation attribute.

The following code example demonstrates enabling XAMLC at the assembly level:

using Xamarin.Forms.Xaml;
...
[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace PhotoApp
{
  ...
}

In this example, compile-time checking of all the XAML contained within the PhotoApp namespace will be performed, with XAML errors being reported at compile-time rather than run-time. The assembly prefix to the XamlCompilation attribute specifies that the attribute applies to the entire assembly.

The following code example demonstrates enabling XAMLC at the class level:

using Xamarin.Forms.Xaml;
...
[XamlCompilation (XamlCompilationOptions.Compile)]
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.

The XamlCompilation attribute and the XamlCompilationOptions enumeration reside in the Xamarin.Forms.Xaml namespace, which must be imported in order to use them.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.