Screen orientation on Xamarin
Apps should adapt to the preferred display orientation of the user. Most apps restrict the screen to portrait orientation, but they should also support landscape orientation. Some users have their devices mounted in a fixed orientation (e.g. on the arm of a wheelchair). Therefore, apps need to support both orientations.
When using Xamarin.Forms, device orientation is set at the project level.
For Android: open
MainActivity.cs
and decorate theMainActivity
class with[Activity (ScreenOrientation = ScreenOrientation.FullUser)]
.For iOS: open
Info.plist
and check allDevice 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;
}
}