Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to add [XamlCompilation(XamlCompilationOptions.Compile)] above every class in a Xamarin Forms App? [closed]

I thought I had read before that this was only needed for the App class but now when I go to the sample tabbed application that is part of the Xamarin Form official templates I see it added above every class.

Can someone please confirm. Just the one class or every class when it is added?

enter image description here

like image 737
Alan2 Avatar asked Dec 13 '18 07:12

Alan2


People also ask

What is the purpose of XAML compiler Xamlc )?

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.

How do I enable Xamlc?

If you want to enable compiled XAML through out your application just set [assembly: XamlCompilation (XamlCompilationOptions. Compile)] just above your namespace in App. cs file as below: [assembly: XamlCompilation (XamlCompilationOptions.

What is xamarin form?

Xamarin. Forms is an open source cross-platform framework from Microsoft for building iOS, Android, & Windows apps with . NET from a single shared codebase. Use Xamarin. Forms built in pages, layouts, and controls to build and design mobile apps from a single API that is highly extensible.


2 Answers

Both are acceptable.

If you want to enable compiled XAML through out your application just set [assembly: XamlCompilation (XamlCompilationOptions.Compile)] just above your namespace in App.cs file as below:

[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace App
{
   ....

}

If you want to enable per file then set it as below at class level.

[XamlCompilation (XamlCompilationOptions.Compile)]
public class TestPage : ContentPage
{
    ....
}

Read out more on this at:

https://devblogs.microsoft.com/xamarin/optimizing-xamarin-forms-apps-for-maximum-performance/

like image 66
Arun Gupta Avatar answered Nov 13 '22 23:11

Arun Gupta


No, you can add an assembly level attribute just once for it to include all your XAML files:

[assembly: XamlCompilation (XamlCompilationOptions.Compile)]

re: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xamlc

using Xamarin.Forms.Xaml;
...
[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace SomeApp
{
  ...
}
like image 36
SushiHangover Avatar answered Nov 13 '22 23:11

SushiHangover