调用Device.RuntimePlatform会导致运行时异常

Tai*_*T's 8 c# portable-class-library xamarin xamarin.forms

我在我的所有项目(平台+共享)中将我的Xamarin.Forms包更新到最新版本(2.3.4.224),现在看来我不应再使用它了Device.OS,TargetPlatform因为它们已被弃用.

由于这些行,编译器抱怨:

if (Device.OS == TargetPlatform.iOS) 
    _API_BASE_URI = "http://XXX.XXX.XXX.XXX"; 
else
    _API_BASE_URI = "http://YYY.YYY.YYY.YYY"; 
Run Code Online (Sandbox Code Playgroud)

它说:

"Device.OS已过时.请改用RuntimePlatform"

到目前为止一直很好,现在我想修复它,我一直在尝试使用:

Debug.WriteLine(Device.RuntimePlatform);
Run Code Online (Sandbox Code Playgroud)

但它抛出了运行时异常.这是堆栈跟踪

04-08 14:57:34.812 I/MonoDroid(3782):UNHANDLED EXCEPTION:04-08 14:57:34.824 I/MonoDroid(3782):System.TypeInitializationException:'Mob.ApiCommunication'的类型初始值设定项引发异常.---> System.MissingMethodException:找不到方法'Xamarin.Forms.Device.get_RuntimePlatform'.04-08 14:57:34.824 I/MonoDroid(3782):---内部异常堆栈跟踪结束--- 04-08 14:57:34.824 I/MonoDroid(3782):at(包装器管理到本机) )System.Object:__ icall_wrapper_mono_generic_class_init(intptr)04-08 14:57:34.824 I/MonoDroid(3782):在Mob.Views.Public.LoginViewModel.RestoreState(System.Collections.Generic.IDictionary`2 [TKey,TValue]字典)[0x00001]在C:\ Users ...\Source ...\LoginViewModel.cs:52 04-08 14:57:34.824 I/MonoDroid(3782):在Mob.App.OnStart()[0x00001]中C:\ Users ...\App.xaml.cs:39 04-08 14:57:34.824 I/MonoDroid(3782):在C:\ BuildAgent2\work中的Xamarin.Forms.Application.SendStart()[0x00000]\ca3766cfc22354a1\Xamarin.Forms.Core\Application.cs:228 04-08 14:57:34.824 I/MonoDroid(3782):at Camarin.Forms.Platform.Android.FormsAppCompatActivity + d__43.MoveNext()[0x0003b] in C :\ BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:426 04-08 14:57:34.824 I/MonoDroid(3782):---来自pre的堆栈跟踪结束 抛出异常的vious位置--- 04-08 14:57:34.824 I/MonoDroid(3782):/ Users/builder/data/lanes/4468中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c] /f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 04-08 14:57:34.824 I/MonoDroid(3782):at System.Runtime.CompilerServices.AsyncMethodBuilderCore. m__0(System.Object state)[0x00000]在/Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 04-08 14:57:34.824 I/MonoDroid(3782):在/ Users/builder/data/lanes/4468/b16fb820/source/xamarin-android/src中的Android.App.SyncContext + c__AnonStorey0.<> m__0()[0x00000] /Mono.Android/Android.App/SyncContext.cs:35 04-08 14:57:34.824 I/MonoDroid(3782):在/ Users/builder /中的Java.Lang.Thread + RunnableImplementor.Run()[0x0000b]数据/通道/ 4468/b16fb820 /源极/ xamarin-机器人/ SRC/Mono.Android/Java.Lan g/Thread.cs:36 04-08 14:57:34.824 I/MonoDroid(3782):在/ Users/builder /中的Java.Lang.IRunnableInvoker.n_Run(System.IntPtr jnienv,System.IntPtr native__this)[0x00009] data/lanes/4468/b16fb820/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Java.Lang.IRunnable.cs:81 04-08 14:57:34.824 I/MonoDroid(3782) ):at(wrapper dynamic-method)System.Object:88db5e57-5ac7-4ba4-a574-4ec5eaf704fd(intptr,intptr)

我是否因使用RuntimePlatform而遗漏了一些东西?我环顾四周,但目前关于Device该类的任何文档/示例都使用了已弃用的成员.

Tai*_*T's 5

我想出了一个解决方案。奇怪的是,在发布到 SO 之前,我已经为我的 Xamarin 项目完成了该操作,但没有任何效果。但这一次,在 Xamarin 论坛上阅读了此主题后:https : //forums.xamarin.com/discussion/92455/xamarin-forms-2-3-4-224这就是我所做的:

我已经关闭了 VisualStudio,从我的解决方案中的所有项目中清除了所有“bin”和“obj”文件夹,然后重新启动了 VS,然后清理并重建了解决方案。

现在Debug.WriteLine(Device.RuntimePlatform);按预期返回“Android”字符串!,


Eka*_*m E 5

Xamarin.Forms-2.3.4.224已将条件检查从:

if (Device.OS == TargetPlatform.iOS) 
{

}
Run Code Online (Sandbox Code Playgroud)

到:

if (Device.RuntimePlatform.Equals("Android"))
{
    Debug.WriteLine($"Onplatform: {Device.RuntimePlatform}");
}
else if (Device.RuntimePlatform.Equals("iOS"))
{
    Debug.WriteLine($"Onplatform: {Device.RuntimePlatform}");
}
Run Code Online (Sandbox Code Playgroud)