Szerkesztés

Megosztás a következőn keresztül:


Device information

Browse sample. Browse the sample

This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) IDeviceInfo interface to read information about the device the app is running on.

The default implementation of the IDeviceInfo interface is available through the DeviceInfo.Current property. Both the IDeviceInfo interface and DeviceInfo class are contained in the Microsoft.Maui.Devices namespace.

Read device info

The IDeviceInfo interface provides many properties that describe the device, such as the manufacturer and idiom. The following example demonstrates reading the device info properties:

private void ReadDeviceInfo()
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    sb.AppendLine($"Model: {DeviceInfo.Current.Model}");
    sb.AppendLine($"Manufacturer: {DeviceInfo.Current.Manufacturer}");
    sb.AppendLine($"Name: {DeviceInfo.Current.Name}");
    sb.AppendLine($"OS Version: {DeviceInfo.Current.VersionString}");
    sb.AppendLine($"Idiom: {DeviceInfo.Current.Idiom}");
    sb.AppendLine($"Platform: {DeviceInfo.Current.Platform}");

    bool isVirtual = DeviceInfo.Current.DeviceType switch
    {
        DeviceType.Physical => false,
        DeviceType.Virtual => true,
        _ => false
    };

    sb.AppendLine($"Virtual device? {isVirtual}");

    DisplayDeviceLabel.Text = sb.ToString();
}

To access the user-assigned device name through the IDeviceInfo.Name property in iOS 16 and later, rather than the generic device name, your app must meet certain criteria and be assigned the com.apple.developer.device-information.user-assigned-device-name entitlement. For more information, see com.apple.developer.device-information.user-assigned-device-name on developer.apple.com.

Get the device platform

The IDeviceInfo.Platform property represents the operating system the app is running on. The DevicePlatform type provides a property for each operating system:

The following example demonstrates checking if the IDeviceInfo.Platform property matches the Android operating system:

private bool IsAndroid() =>
    DeviceInfo.Current.Platform == DevicePlatform.Android;

Get the device type

The IDeviceInfo.Idiom property represents the type of device the app is running on, such as a desktop computer or a tablet. The DeviceIdiom type provides a property for each type of device:

The following example demonstrates comparing the IDeviceInfo.Idiom value to a DeviceIdiom property:

private void PrintIdiom()
{
    if (DeviceInfo.Current.Idiom == DeviceIdiom.Desktop)
        Console.WriteLine("The current device is a desktop");
    else if (DeviceInfo.Current.Idiom == DeviceIdiom.Phone)
        Console.WriteLine("The current device is a phone");
    else if (DeviceInfo.Current.Idiom == DeviceIdiom.Tablet)
        Console.WriteLine("The current device is a Tablet");
}

Device type

IDeviceInfo.DeviceType property an enumeration to determine if the application is running on a physical or virtual device. A virtual device is a simulator or emulator.

bool isVirtual = DeviceInfo.Current.DeviceType switch
{
    DeviceType.Physical => false,
    DeviceType.Virtual => true,
    _ => false
};

Platform differences

This section describes the platform-specific differences with the device information.

No platform differences.