Screen orientation - Xamarin

When using Xamarin.Forms, device orientation is set at the project level.

  • For Android: open MainActivity.cs and decorate the MainActivity class with [Activity (ScreenOrientation = ScreenOrientation.FullUser)].
  • For iOS: open Info.plist and check all Device Orientation checkboxes.

You can listen to orientation changes by using the DeviceDisplay class included in Xamarin.Essentials.

public class OrientationChanges
{
    public OrientationChanges()
    {
        // Subscribe to changes of screen metrics
        DeviceDisplay.MainDisplayInfoChanged += OnMainDisplayInfoChanged;
    }

    void OnMainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs  e)
    {
        // Process changes
        var displayInfo = e.DisplayInfo;
    }
}