XamlCompilation In Xamarin.Forms

If we write large code in a XAML page, we have the possibility of getting some syntax errors but we will be unable to catch those XAML page issues until we run the application. It's a very bad experience for users.
 
To avoid this issue and catch XAML page related issues at compile time itself, Xamarin introduced  'XamlCompilation'. If we add this attribute in our xaml.cs file we can get XAML page related errors at compile time.
 
Here is the sample code for how to add this attribute to our XAML page.
 
FirstPage
  1.  <?xml version="1.0" encoding=“utf-8" ?>  
  2.  <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.   x:Class="SampleProject.FirstPage">  
  5.    
  6.   <Button Text="MoveToNextPage" HeightRequest = "40" WidthRequest = "150"   
  7. VerticalOption="Center" HorizontalOptions="Center" OnClick = "On_Clicked"/>  
  8. </ContentPage>     
FirstPage.cs
  1. namespace SampleProject  
  2. {  
  3.    public partial class SamplePage : ContentPage  
  4.    {  
  5.       public MainPage()  
  6.       {  
  7.          InitializeComponent();  
  8.       }  
  9.    
  10.       Public void On_Clicked(Object sender, EventArgs e)  
  11.       {  
  12.          Navigation.PushAsync(new SamplePage());  
  13.       }  
  14.    }  
  15. }  
SamplePage
  1. <?xml version="1.0" encoding=“utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4. x:Class="SampleProject.SamplePage">  
  5.    
  6. <Label Text="Welcome to Xamarin Forms!" VerticalOption="Center" HorizontalOptions="Center" />  
  7. </ContentPage>  
SamplePage.cs
  1. namespace SampleProject  
  2. {  
  3.    public partial class SamplePage : ContentPage  
  4.    {  
  5.       public MainPage()  
  6.       {  
  7.          InitializeComponent();  
  8.       }  
  9.    }  
  10. }  
In the above code, the sample XAML page contains one error; i.e., we write 'VerticalOption' instead of 'VerticalOptions' but we do not get any error up to run the application and open this page. When user runs the application it will open successfully and show the first page of the application without getting any problem. When a user clicks on 'MoveToPage' button on the first page it will navigate to SamplePage and application will crash due to an error in XAML page. We can avoid these type of errors by adding "XamlCompilation" attribute like below to our xaml.cs file.
 
SamplePage.cs
  1. [assembly: XamlCompilation(XamlCompilationOptions.Compile)] //  
  2. namespace SampleProject  
  3. {  
  4.    public partial class SamplePage : ContentPage  
  5.    {  
  6.       public MainPage()  
  7.       {  
  8.           InitializeComponent();  
  9.       }  
  10.    }  
After adding this attribute to your xaml.cs file and running the application you will get the error at compile time itself. So this attribute is greatly helpful for us if we write large XAML pages and try to find out what is missing in that file for resolving those issues.