次の方法で共有


デバイス情報

サンプルを参照します。

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

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

デバイス情報を読み取る

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

iOS 16以降では、一般的なデバイス名ではなく、ユーザーが割り当てたデバイス名にアクセスするには、アプリがある特定の基準を満たし、com.apple.developer.device-information.user-assigned-device-nameのエンタイトルメントが割り当てされている必要があります。この為には、プロパティ〈iDeviceInfo.Name〉を通じてアクセスを試みる必要があります。 詳細については、developer.apple.com の com.apple.developer.device-information.user-assigned-device-name をご覧ください。

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

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

次の例は、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");
}

デバイスの種類

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

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

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

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

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