Device display information
This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) IDeviceDisplay interface to read information about the device's screen metrics. This interface can be used to request the screen stays awake while the app is running.
The default implementation of the IDeviceDisplay
interface is available through the DeviceDisplay.Current property. Both the IDeviceDisplay
interface and DeviceDisplay
class are contained in the Microsoft.Maui.Devices
namespace.
Main display info
The MainDisplayInfo property returns information about the screen and orientation. The following code example uses the Loaded event of a page to read information about the current screen:
private void ReadDeviceDisplay()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine($"Pixel width: {DeviceDisplay.Current.MainDisplayInfo.Width} / Pixel Height: {DeviceDisplay.Current.MainDisplayInfo.Height}");
sb.AppendLine($"Density: {DeviceDisplay.Current.MainDisplayInfo.Density}");
sb.AppendLine($"Orientation: {DeviceDisplay.Current.MainDisplayInfo.Orientation}");
sb.AppendLine($"Rotation: {DeviceDisplay.Current.MainDisplayInfo.Rotation}");
sb.AppendLine($"Refresh Rate: {DeviceDisplay.Current.MainDisplayInfo.RefreshRate}");
DisplayDetailsLabel.Text = sb.ToString();
}
The IDeviceDisplay interface also provides the MainDisplayInfoChanged event that is raised when any screen metric changes, such as when the device orientation changes from DisplayOrientation.Landscape to DisplayOrientation.Portrait.
Keep the screen on
You can also prevent the device from locking or the screen turning off by setting the KeepScreenOn property to true
. The following code example toggles the screen lock whenever the switch control is pressed:
private void AlwaysOnSwitch_Toggled(object sender, ToggledEventArgs e) =>
DeviceDisplay.Current.KeepScreenOn = AlwaysOnSwitch.IsToggled;
Platform differences
This section describes the platform-specific differences with the device display.
No platform differences.