如何在同一类中使用 Android.App.Application 和 Xamarin.Forms.Applicatio



我收到一条错误消息,因为我在同一类中有以下两行代码:

using Xamarin.Forms;
using Android.App;

错误CS0104:"应用程序"是"Android.App.Application"和"Xamarin.Forms.Application"之间的不明确引用

 if (Device.RuntimePlatform == Device.Android)
   deviceID = Settings.Secure.GetString(Application.Context.ContentResolver, Settings.Secure.AndroidId);

如何使用 Android.App 来执行代码?我需要设备 ID。我不能删除"using Xamarin.Forms",因为我在课堂上使用它。

您可以为命名空间分配别名

using xf = Xamarin.Forms;
using aa = Android.App;
...
aa.Application.Context.ContentResolver...

或者,您可以在代码中只使用完全限定的命名空间

Android.App.Application.Context.ContentResolver...

或者,如果项目命名空间与另一个命名空间冲突,请使用 global 关键字

global::Android.App.Application.Context.ContentResolver...

最新更新