如何在Xamarin Forms中获取当前设备模型?

6
我写了一个记录器类,当我的应用程序出现异常时会向我发送电子邮件,但是我想知道抛出异常的设备型号是什么。例如:
Logger.LogError(App.ApplicationName, Device.OS.ToString(), "Error getting passenger ancillary transactions!", Settings.CurrentUsername, ex.ToString());

发送一封带有应用程序名称和设备是“Android”或“iOS”的异常邮件,但没有比这更具体的信息。在Visual Studio中,它会显示我将要在哪个设备上进行调试(如“iPod Touch”或“LG LS991”)。是否有一种方法可以访问此信息?我在Xamarin文档中找不到任何信息。

2个回答

11

Xamarin.Plugins由jamesmontemagno制作,可跨平台在Xamarin.Forms中使用:

您需要寻找的是DeviceInfo插件:

https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/DeviceInfo

Nuget搜索:'Device Info Plugin for Xamarin and Windows'

设备型号

/// <summary>
/// Get the model of the device
/// </summary>
string Model { get; }

版本

/// <summary>
/// Get the version of the Operating System
/// </summary>
string Version { get; }
Returns the specific version number of the OS such as:

iOS: 8.1
Android: 4.4.4
Windows Phone: 8.10.14219.0
WinRT: always 8.1 until there is a work around

平台

/// <summary>
/// Get the platform of the device
/// </summary>

Platform { get; }

Returns the Platform Enum of:

public enum Platform
{
  Android,
  iOS,
  WindowsPhone,
  Windows
}

只是想补充一下,CrossDeviceInfo.Current.Model 可以得到实际的模型字符串。 - aufty

5

为了完善SushiHangover的回答:

在iOS上,Model属性并不包含完整的型号名称(例如,您无法区分iPhone 3和iPhone 4)。

幸运的是,这里有一些代码可以为您提取它。


我该如何在Xamarin Forms中使用它? - Pxaml

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接