Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Device.RuntimePlatform causes runtime exception

I updated my Xamarin.Forms package to the latest (2.3.4.224) in all my projects (platform+shared) and it seems now I shouldn't use anymore Device.OS nor TargetPlatform enum as they are deprecated.

The compiler is complaining because of these lines:

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

It say's:

"Device.OS is obsolete. Use RuntimePlatform instead"

So far so good, now I want to fix that and I've been trying using:

Debug.WriteLine(Device.RuntimePlatform);

But it's throwing a runtime exception. Here's the stacktrace

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)

Am I missing something with the use of RuntimePlatform? I've looked around, but currently any documentation/sample regarding the Device class is using the deprecated members.

like image 285
TaiT's Avatar asked Apr 08 '17 13:04

TaiT's


2 Answers

I came out with a solution. What is strange is that before posting on SO, I've already done that for my Xamarin projects and it didn't have any effect. But this time, after reading this thread on Xamarin forums: https://forums.xamarin.com/discussion/92455/xamarin-forms-2-3-4-224 that's what I did:

I've closed VisualStudio, cleaned all "bin" & "obj" folders from all projects in my solution, then restarted VS and then cleaned & rebuilt solution.

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

like image 109
TaiT's Avatar answered Nov 18 '22 10:11

TaiT's


Xamarin.Forms-2.3.4.224 has changed the condition checking from:

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}");
}
like image 5
Ekambaram E Avatar answered Nov 18 '22 11:11

Ekambaram E