设备信息

您所在的位置:网站首页 安卓解析软件包 设备信息

设备信息

2023-05-12 06:28| 来源: 网络整理| 查看: 265

设备信息 项目 05/05/2023

本文介绍如何使用 .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 为每个操作系统提供属性:

DevicePlatform.Android DevicePlatform.iOS DevicePlatform.macOS DevicePlatform.MacCatalyst DevicePlatform.tvOS DevicePlatform.Tizen DevicePlatform.WinUI DevicePlatform.watchOS DevicePlatform.Unknown

以下示例演示如何检查 属性是否 IDeviceInfo.Platform 与操作系统匹配 Android :

private bool IsAndroid() => DeviceInfo.Current.Platform == DevicePlatform.Android; 获取设备类型

属性 IDeviceInfo.Idiom 表示运行应用的设备类型,例如台式计算机或平板电脑。 类型 DeviceIdiom 为每种类型的设备提供属性:

DeviceIdiom.Phone DeviceIdiom.Tablet DeviceIdiom.Desktop DeviceIdiom.TV DeviceIdiom.Watch DeviceIdiom.Unknown

以下示例演示如何将 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 }; 平台差异

本部分介绍与设备信息之间的平台特定差异。

Android iOS/Mac Catalyst Windows

无平台差异。

若要通过 IDeviceInfo.Name iOS 16 及更高版本中的 属性(而不是通用设备名称)访问用户分配的设备名称,应用必须满足特定条件并分配 com.apple.developer.device-information.user-assigned-device-name 权利。 有关详细信息,请参阅 com.apple.developer.device-information.user-assigned-device-name developer.apple.com。

iOS 不会向开发人员公开 API 来获取特定 iOS 设备的模型。 而是返回硬件标识符,例如 iPhone10,6,它引用 iPhone X。这些标识符的映射不由 Apple 提供,但可以在 Internet 上找到,例如 iPhone Wiki 和 获取 iOS 模型 网站。

无平台差异。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3