Share via


Xamarin.Essentials: Device Display Information

The DeviceDisplay class provides information about the device's screen metrics the application is running on and can request to keep the screen from falling asleep when the application is running.

Get started

To start using this API, read the getting started guide for Xamarin.Essentials to ensure the library is properly installed and set up in your projects.

Using DeviceDisplay

Add a reference to Xamarin.Essentials in your class:

using Xamarin.Essentials;

Main Display Info

In addition to basic device information the DeviceDisplay class contains information about the device's screen and orientation.

// Get Metrics
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;

// Orientation (Landscape, Portrait, Square, Unknown)
var orientation = mainDisplayInfo.Orientation;

// Rotation (0, 90, 180, 270)
var rotation = mainDisplayInfo.Rotation;

// Width (in pixels)
var width = mainDisplayInfo.Width;

// Height (in pixels)
var height = mainDisplayInfo.Height;

// Screen density
var density = mainDisplayInfo.Density;

The DeviceDisplay class also exposes an event that can be subscribed to that is triggered whenever any screen metric changes:

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

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

Keep Screen On

The DeviceDisplay class exposes a bool property called KeepScreenOn that can be set to attempt to keep the device's display from turning off or locking.

public class KeepScreenOnTest
{
    public void ToggleScreenLock()
    {
        DeviceDisplay.KeepScreenOn = !DeviceDisplay.KeepScreenOn;
    }
}

Platform Differences

No differences.

API

Find more Xamarin videos on Channel 9 and YouTube.