Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Device.OnPlatform deprecated

Tags:

c#

xamarin

Inside the constructor of my ContentPage I try to set a platform dependent padding value:

Padding = new Thickness(5, Device.OnPlatform(20, 5, 5), 5, 5);

Visual Studio underlines Device.OnPlatform and when I hover the mouse pointer over the method call I get the following warning:

Devide.OnPlatform(T, T, T) is obsolete: 'Use switch(RuntimePlatform) instead'.

The code initially used is from the e-book 'Creating Mobile Apps with Xamarin.Forms Book' published in 2016. I 'm really surprised how fast this platform evolves!

Unfortunately I'm not aware of how Device.OnPlatform should be replaced using the way suggested by the warning.

like image 277
Giorgos Betsos Avatar asked Aug 05 '17 20:08

Giorgos Betsos


2 Answers

2016 was the year this method became deprecated.

You're supposed to use a switch statement to determine the OS.

switch(Device.RuntimePlatform)
{
    case Device.iOS:
      return new Thickness(5, 5, 5, 0)
    default:
      return new Thickness(5, 5, 5, 0)
 }

You can of course wrap this inside a function which will do the same job as you wished to do with Device.OnPlatform, but instead of calling Device.OnPlatform you'll call your own function.

like image 138
Adrian Avatar answered Oct 21 '22 21:10

Adrian


I resolved this by using inline ternary conditional operator, this assumes you want something different for just iOS

Padding = new Thickness(5, (Device.RuntimePlatform==Device.iOS?20:5), 5, 5);
like image 32
gattsbr Avatar answered Oct 21 '22 22:10

gattsbr