A call to Device.RuntimePlatform raises an exception at runtime

I updated my Xamarin.Forms package to the last (2.3.4.224) in all my projects (platform + general), and now I no longer need to use Device.OS and TargetPlatform enum, since they are deprecated.

The compiler complains about these lines:

 if (Device.OS == TargetPlatform.iOS) _API_BASE_URI = "http://XXX.XXX.XXX.XXX"; else _API_BASE_URI = "http://YYY.YYY.YYY.YYY"; 

He says:

"Device.OS is deprecated. Use RuntimePlatform instead.

So far so good, now I want to fix it, and I tried to use:

 Debug.WriteLine(Device.RuntimePlatform); 

But this throws an exception at runtime. Here stacktrace

04-08 14: 57: 34.812 I / MonoDroid (3782): WRONG EXCLUSION: 04-08 14: 57: 34.824 I / MonoDroid (3782): System.TypeInitializationException: The type initializer for "Mob.ApiCommunication" is an exception. ---> System.MissingMethodException: method "Xamarin.Forms.Device.get_RuntimePlatform" was not found. 04-08 14: 57: 34.824 I / MonoDroid (3782): --- End of the internal exception stack trace --- 04-08 14: 57: 34.824 I / MonoDroid (3782): at (wrapper succeeded to the native) System. Object: __ icall_wrapper_mono_generic_class_init (intptr) 04-08 14: 57: 34.824 I / MonoDroid (3782): with 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): on Mob.App.OnStart () [0x00001] in C: \ Users ... \ App.xaml.cs: 39 04-08 14: 57: 34.824 I / MonoDroid (3782): when Xamarin.Forms.Application.SendStart () [0x00000] in C: \ BuildAgent2 \ works \ ca3766cfc22354a1 \ Xamarin.Forms.Core \ Application.cs: 228 04-08 14: 57: 34.824 I / MonoDroid (3782): with Xamarin.Forms.Platform.Android.FormsAppCompatActivity + d__43.MoveNext () [0x0003b] in C: \ BuildAgent2 \ works \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ Ap pCompat \ FormsAppCompatActivity.cs: 426 04-08 14: 57: 34.824 I / MonoDroid (3782): --- The end of the stack trace from the previous location where the exception was thrown --- 04-08 14: 57: 34,824 I / MonoDroid (3782): when 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): when 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): with 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): with 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): with 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 (dynamic wrapper method) System.Object: 88db5e57-5ac7-4ba4-a574-4ec5eaf704fd (intptr, intptr)

Am I missing something using RuntimePlatform? I looked around, but currently any documentation / sample related to the Device class uses obsolete elements.

+8
c # portable-class-library xamarin xamarin.forms
source share
3 answers

I came out with a solution. The strange thing is that before publishing on SO, I already did this for my Xamarin projects, and it had no effect. But this time, after reading this thread on the Xamarin forums: https://forums.xamarin.com/discussion/92455/xamarin-forms-2-3-4-224 , what I did:

I closed VisualStudio, cleared all the bin and obj folders from all the projects in my solution , then restarted VS, and then cleared & restored solution.

Now Debug.WriteLine(Device.RuntimePlatform); returns the string "Android" as expected !,

+4
source share

Xamarin.Forms-2.3.4.224 changed the condition check to:

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

To:

 if (Device.RuntimePlatform.Equals("Android")) { Debug.WriteLine($"Onplatform: {Device.RuntimePlatform}"); } else if (Device.RuntimePlatform.Equals("iOS")) { Debug.WriteLine($"Onplatform: {Device.RuntimePlatform}"); } 
+4
source share

hvaughan3 comment on OP is what I did for me. Make sure that not only your PCLs, but also your Android / iOS / etc projects have updated their packages. Then do a full cleanup and build from there after the accepted OP answer.

0
source share

All Articles