我正在嘗試使用我正在使用的移動 xamarin 應用程式檢索移動設備的唯一 ID
MainActivity.Cs
public class AndroidDevice : IDevice
{
public string GetIdentifier()
{
var context = Android.App.Application.Context;
return Android.Provider.Settings.Secure.GetString(context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);
}
}
我在共享專案中創建了一個介面作為
public interface IDevice
{
string GetIdentifier();
}
我將 shaed 專案中的函式稱為
string deviceIdentifier = DependencyService.Get<IDevice>().GetIdentifier();
我知道
System.NullReferenceException: 'Object reference not set to an instance of an object.'
uj5u.com熱心網友回復:
您需要將 Dependency 屬性添加到宣告實作介面的類的程式集命名空間。
例如。
[assembly: Dependency(typeof(DroidImplementation))]
namespace MyApp.Droid.Implementation
{
public class DroidImplementation : IMyInterface
{
// your class implementation here
}
}
我不會將該類添加到 MainActivity.cs 而是為此創建一個單獨的命名空間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/391094.html
上一篇:嘗試訪問Xamarin.FormsAndroid上的服務器時拋出System.AggregateException
