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

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/


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
{
  ...
}