调用 Device.RuntimePlatform 导致运行时异常
Calling Device.RuntimePlatform causes runtime exception
我在所有项目(平台+共享)中将我的 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 is obsolete. Use RuntimePlatform instead"
到目前为止一切顺利,现在我想解决这个问题,我一直在尝试使用:
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:
The type initializer for 'Mob.ApiCommunication' threw an exception.
---> System.MissingMethodException: Method 'Xamarin.Forms.Device.get_RuntimePlatform' not found. 04-08
14:57:34.824 I/MonoDroid( 3782): --- End of inner exception stack
trace --- 04-08 14:57:34.824 I/MonoDroid( 3782): at (wrapper
managed-to-native)
System.Object:__icall_wrapper_mono_generic_class_init (intptr) 04-08
14:57:34.824 I/MonoDroid( 3782): at
Mob.Views.Public.LoginViewModel.RestoreState
(System.Collections.Generic.IDictionary`2[TKey,TValue] dictionary)
[0x00001] in
C:\Users...\Source...\LoginViewModel.cs:52
04-08 14:57:34.824 I/MonoDroid( 3782): at Mob.App.OnStart ()
[0x00001] in
C:\Users...\App.xaml.cs:39 04-08
14:57:34.824 I/MonoDroid( 3782): at
Xamarin.Forms.Application.SendStart () [0x00000] in
C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Application.cs:228
04-08 14:57:34.824 I/MonoDroid( 3782): at
Xamarin.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): --- End of stack trace from
previous location where exception was thrown --- 04-08 14:57:34.824
I/MonoDroid( 3782): at
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
[0x0000c] in
/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): at
System.Runtime.CompilerServices.AsyncMethodBuilderCore.m__0
(System.Object state) [0x00000] in
/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): at
Android.App.SyncContext+c__AnonStorey0.<>m__0 () [0x00000] in
/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): at
Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in
/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): at
Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr
native__this) [0x00009] in
/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): at (wrapper dynamic-method)
System.Object:88db5e57-5ac7-4ba4-a574-4ec5eaf704fd (intptr,intptr)
我在使用 RuntimePlatform 时是否遗漏了什么?我环顾四周,但目前任何关于 Device
class 的 documentation/sample 都在使用已弃用的成员。
我想出了一个解决方案。奇怪的是,在 SO 上发帖之前,我已经为我的 Xamarin 项目做了这件事,但没有任何效果。但这一次,在阅读 Xamarin 论坛上的这个帖子后:https://forums.xamarin.com/discussion/92455/xamarin-forms-2-3-4-224 这就是我所做的:
I've closed VisualStudio, cleaned all "bin" & "obj" folders from all
projects in my solution, then restarted VS and then cleaned &
rebuilt solution.
现在 Debug.WriteLine(Device.RuntimePlatform);
returns "Android" 字符串符合预期!,
Xamarin.Forms-2.3.4.224 已更改条件检查自:
if (Device.OS == TargetPlatform.iOS)
{
}
收件人:
if (Device.RuntimePlatform.Equals("Android"))
{
Debug.WriteLine($"Onplatform: {Device.RuntimePlatform}");
}
else if (Device.RuntimePlatform.Equals("iOS"))
{
Debug.WriteLine($"Onplatform: {Device.RuntimePlatform}");
}
hvaughan3 对 OP 的评论对我有用。确保不仅您的 PCL 项目而且 Android/iOS/etc 项目也更新了它们的包。然后按照 OP 接受的答案进行全面清理并从那里构建。
我使用以下没有任何问题
if(Device.RuntimePlatform.Equals("Android")){
//YOUR CODE
}else if(Device.RuntimePlatform.Equals("iOS")){
//YOUR CODE
}
看起来 VS 标准清理将其关闭并再次打开答案是解决方案...
我在所有项目(平台+共享)中将我的 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 is obsolete. Use RuntimePlatform instead"
到目前为止一切顺利,现在我想解决这个问题,我一直在尝试使用:
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: The type initializer for 'Mob.ApiCommunication' threw an exception. ---> System.MissingMethodException: Method 'Xamarin.Forms.Device.get_RuntimePlatform' not found. 04-08 14:57:34.824 I/MonoDroid( 3782): --- End of inner exception stack trace --- 04-08 14:57:34.824 I/MonoDroid( 3782): at (wrapper managed-to-native) System.Object:__icall_wrapper_mono_generic_class_init (intptr) 04-08 14:57:34.824 I/MonoDroid( 3782): at Mob.Views.Public.LoginViewModel.RestoreState (System.Collections.Generic.IDictionary`2[TKey,TValue] dictionary) [0x00001] in C:\Users...\Source...\LoginViewModel.cs:52 04-08 14:57:34.824 I/MonoDroid( 3782): at Mob.App.OnStart () [0x00001] in C:\Users...\App.xaml.cs:39 04-08 14:57:34.824 I/MonoDroid( 3782): at Xamarin.Forms.Application.SendStart () [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Application.cs:228 04-08 14:57:34.824 I/MonoDroid( 3782): at Xamarin.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): --- End of stack trace from previous location where exception was thrown --- 04-08 14:57:34.824 I/MonoDroid( 3782): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /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): at System.Runtime.CompilerServices.AsyncMethodBuilderCore.m__0 (System.Object state) [0x00000] in /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): at Android.App.SyncContext+c__AnonStorey0.<>m__0 () [0x00000] in /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): at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in /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): at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in /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): at (wrapper dynamic-method) System.Object:88db5e57-5ac7-4ba4-a574-4ec5eaf704fd (intptr,intptr)
我在使用 RuntimePlatform 时是否遗漏了什么?我环顾四周,但目前任何关于 Device
class 的 documentation/sample 都在使用已弃用的成员。
我想出了一个解决方案。奇怪的是,在 SO 上发帖之前,我已经为我的 Xamarin 项目做了这件事,但没有任何效果。但这一次,在阅读 Xamarin 论坛上的这个帖子后:https://forums.xamarin.com/discussion/92455/xamarin-forms-2-3-4-224 这就是我所做的:
I've closed VisualStudio, cleaned all "bin" & "obj" folders from all projects in my solution, then restarted VS and then cleaned & rebuilt solution.
现在 Debug.WriteLine(Device.RuntimePlatform);
returns "Android" 字符串符合预期!,
Xamarin.Forms-2.3.4.224 已更改条件检查自:
if (Device.OS == TargetPlatform.iOS)
{
}
收件人:
if (Device.RuntimePlatform.Equals("Android"))
{
Debug.WriteLine($"Onplatform: {Device.RuntimePlatform}");
}
else if (Device.RuntimePlatform.Equals("iOS"))
{
Debug.WriteLine($"Onplatform: {Device.RuntimePlatform}");
}
hvaughan3 对 OP 的评论对我有用。确保不仅您的 PCL 项目而且 Android/iOS/etc 项目也更新了它们的包。然后按照 OP 接受的答案进行全面清理并从那里构建。
我使用以下没有任何问题
if(Device.RuntimePlatform.Equals("Android")){
//YOUR CODE
}else if(Device.RuntimePlatform.Equals("iOS")){
//YOUR CODE
}
看起来 VS 标准清理将其关闭并再次打开答案是解决方案...