我将我的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";
这段话的意思是:
"Device.OS已经过时了。请使用RuntimePlatform代替"
到目前为止一切都很好,现在我想修复它,我一直在尝试使用:
Debug.WriteLine(Device.RuntimePlatform);
但是它抛出了一个运行时异常。以下是堆栈跟踪信息。
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): 在 (wrapper managed-to-native) 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] dictionary)[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): 在 Xamarin.Forms.Application.SendStart ()[0x00000]中的C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Application.cs:228 04-08 14:57:34.824 I/MonoDroid( 3782): 在 Xamarin.Forms.Platform.Android.FormsAppCompatActivity+d__43.MoveNext ()[0x0003b]中的C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:426 04-08 14:57:34.824 I/MonoDroid( 3782): --- 前一个位置引发异常的堆栈跟踪结束 --- 04-08 14:57:34.824 I/MonoDroid( 3782): 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()[0x0000c]中的/Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 04-08 14:57:34.824 I/MonoDroid( 3782): 在 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): 在 Android.App.SyncContext+c__AnonStorey0.<>m__0 ()[0x00000]中的/Users/builder/data/lanes/4468/b16fb820/source/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:35 04-08 14:57:34.824 I/MonoDroid( 3782): 在 Java.Lang.Thread+RunnableImplementor.Run ()[0x0000b]中的/Users/builder/data/lanes/4468/b16fb820/source/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36 04-08 14:57:34.824 I/MonoDroid( 3782): 在 Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this)[0x00009]中的/Users/builder/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): 在 (wrapper dynamic-method) System.Object:88db5e57-5ac7-4ba4-a574-4ec5eaf704fd (intptr,intptr)
我在使用RuntimePlatform时是否漏掉了什么?我查看了相关文档和样例,但目前所有关于Device
类的资料都在使用已废弃的成员。
bool isAndroid = Device.RuntimePlatform == Device.Android;
此外,这里有非官方文档:https://github.com/xamarin/Xamarin.Forms/pull/658 - hvaughan3