【发布时间】:2020-01-14 10:55:53
【问题描述】:
我目前使用 Xamarin Forms。我需要为特定的 iPhone 版本编写一段代码。类似if (iphone == 5S) then do something。
我如何在 Xamarin Studio (Forms) 中实现这一点。
【问题讨论】:
标签: ios xamarin.ios xamarin.forms
我目前使用 Xamarin Forms。我需要为特定的 iPhone 版本编写一段代码。类似if (iphone == 5S) then do something。
我如何在 Xamarin Studio (Forms) 中实现这一点。
【问题讨论】:
标签: ios xamarin.ios xamarin.forms
您可以使用this插件获取设备型号。一个好主意是使用Debug.WriteLine() 打印设备模型以查看该方法返回的内容。
var model = CrossDeviceInfo.Current.Model;
Debug.Write(model);
if (model == "MySmartPhoneModel)
{
// Your method
}
【讨论】:
CrossDeviceInfo.Current.Model 只为所有具有 iphone 习语的 iOS 设备返回“iPhone”。它没有给你确切的模型。
这也可以通过使用 DependencyService 来实现。 DependencyService 允许您访问本机功能。 https://developer.xamarin.com/guides/xamarin-forms/dependency-service/
通过使用UIDevice.CurrentDevice.Model,在您的iOS项目中,您可以检索用户设备的型号。
【讨论】:
UIDevice.CurrentDevice.Model 只为所有具有 iphone 习语的 iOS 设备(即不是 iPad)返回“iPhone”。它不会给你确切的模型。