8
votes

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.

4
just to note, I currently get the same so it's not your config. Nothing in the release notes to suggest this change either. I'm going to stick with the Device.Os as it does still work as of writing thisApp Pack
The following is working for me just fine using Xamarin Forms version 2.3.4.224: bool isAndroid = Device.RuntimePlatform == Device.Android; Also here is unofficial documentation: https://github.com/xamarin/Xamarin.Forms/pull/658hvaughan3
@hvaughan3: Unfortunatly it's still the same.TaiT's
Are you sure that your Android project is using the same version of Xamarin Forms as your shared/PCL project? Do you get the same error when you run the iOS project?hvaughan3
Yes I am sure! And yes same error on both iOS and Android!TaiT's

4 Answers

5
votes

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!,

5
votes

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}");
}
0
votes

hvaughan3's comment on the OP is what did it for me. Make sure that not only your PCL but also your Android/iOS/etc projects have their packages updated as well. Then do a full clean and build from there following OP's accepted answer.

0
votes

I use the following without any issues

if(Device.RuntimePlatform.Equals("Android")){
  //YOUR CODE
}else if(Device.RuntimePlatform.Equals("iOS")){
  //YOUR CODE
}

Looks like the VS standard clean turn it off and on again answer is the solution though...