我在项目中Plugin.DeviceInfo
添加了引用,并使用CrossDevice.Device.DeviceId
在代码中获取IMEI,但它抛出异常"plugin.DeviceInfo.CrossDevice
的类型初始值设定项抛出了异常"。 如何解决这个问题。
试试这个:
[assembly: Xamarin.Forms.Dependency(typeof(UniqueIdAndroid))]
namespace UniqueId.Droid
{
public class UniqueIdAndroid : IDevice
{
public string GetIdentifier()
{
return Settings.Secure.GetString(Forms.Context.ContentResolver, Settings.Secure.AndroidId);
}
}
}
然后
string deviceIdentifier = DependencyService.Get<IDevice>().GetIdentifier();