MAUI怎么获取设备信息 MAUI DeviceInfo使用方法

来源:这里教程网 时间:2026-02-21 17:39:57 作者:

在 .NET MAUI 中获取设备信息,主要通过 Microsoft.Maui.ApplicationModel.DeviceInfo 类实现,无需第三方库或平台特定代码,开箱即用。

基础设备信息获取

DeviceInfo 提供跨平台的只读属性,涵盖系统、设备和运行环境的关键标识:

DeviceInfo.Platform:返回
Platforms.iOS
Platforms.Android
Platforms.WinUI
Platforms.MacCatalyst
,用于条件逻辑分支
DeviceInfo.Idiom:识别设备形态,如
DeviceIdiom.Phone
DeviceIdiom.Tablet
DeviceIdiom.Desktop
,适配 UI 布局很实用
DeviceInfo.ManufacturerDeviceInfo.Model:返回厂商名(如 "Samsung")与型号(如 "SM-S918B"),注意 Android 上可能被厂商定制覆盖 DeviceInfo.VersionString:系统版本号字符串(如 "17.6.1" 或 "14"),比
DeviceInfo.Version
(Version 对象)更易读

iOS 用户自定义设备名支持

从 iOS 16 起,若需获取用户在「设置 → 通用 → 关于本机 → 名称」中设置的设备名(而非默认的 "iPhone" 或 "iPad"),需额外配置:

Entitlements.plist
中启用
com.apple.developer.device-information.user-assigned-device-name
权限
调用 DeviceInfo.Name 属性才返回用户设置的名称;否则仍返回系统默认名 该权限仅影响 iOS,Android 和其他平台直接返回设备名(如 Manufacturer + Model 组合)

屏幕与方向辅助判断

DeviceInfo 本身不提供屏幕尺寸或方向,但可配合其他 API 构建完整设备画像:

DeviceInfo.Idiom
判断是手机还是平板,再结合
WindowSize
DisplayInfo.Current.Width
做精细适配
监听
DisplayInfo.MainDisplayInfoChanged
事件响应横竖屏切换
避免依赖
DeviceInfo.Platform == Platforms.Android
做所有判断,应优先用
Idiom
和实际屏幕指标,更符合响应式设计原则

注意事项与常见问题

部分属性在模拟器/仿真器中返回值受限或不准确:

Android 模拟器常返回 "Google" 和 "sdk_gphone64_x86_64",不代表真实设备 iOS 模拟器的
DeviceInfo.Name
默认为 "iPhone Simulator",即使配置了 entitlement 也不生效
真机调试时,
DeviceInfo.Idiom
在折叠屏设备上可能返回
Phone
Tablet
,取决于当前展开状态,建议搭配
FoldableInfo
使用

基本上就这些。用好 DeviceInfo 的核心是区分“平台类型”和“设备形态”,别把 Platform 当成 UI 适配唯一依据。

相关推荐