デバイス情報

Browse sample. サンプルを参照する

この記事では、.NET マルチプラットフォーム アプリ UI (.NET MAUI) IDeviceInfo インターフェイスを使用して、アプリが実行されているデバイスに関する情報を読み取る方法について説明します。

IDeviceInfo インターフェイスの既定の実装は、DeviceInfo.Current プロパティを通じて利用できます。 IDeviceInfo インターフェイスと DeviceInfo クラスはどちらも Microsoft.Maui.Devices 名前空間に含まれています。

デバイス情報を読み取る

IDeviceInfo インターフェイスは、製造元や慣用句など、デバイスを説明する多くのプロパティを提供します。 次の例は、デバイス情報プロパティの読み取りを示しています。

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();
}

デバイス プラットフォームを取得する

IDeviceInfo.Platform プロパティは、アプリが実行されているオペレーティング システムを表します。 DevicePlatform 型は、各オペレーティング システムのプロパティを提供します。

次の例は、IDeviceInfo.Platform プロパティが Android オペレーティング システムと一致するかどうかを確認する方法を示しています。

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

デバイスの種類を取得する

IDeviceInfo.Idiom プロパティは、デスクトップ コンピューターやタブレットなど、アプリが実行されているデバイスの種類を表します。 DeviceIdiom 型は、デバイスの種類ごとにプロパティを提供します。

次の例は、IDeviceInfo.Idiom 値と DeviceIdiom プロパティの比較を示しています。

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 プロパティは、アプリケーションが物理デバイス上で実行されているか仮想デバイス上で実行されているかを判断するための列挙です。 仮想デバイスは、シミュレーターやエミュレーターです。

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

プラットフォームによる違い

このセクションでは、プラットフォーム固有のデバイス情報の違いについて説明します。

プラットフォームによる違いはありません。