Device.OnPlatform deprecated

Inside the constructor of my ContentPage I am trying to set the add-on value for the platform:

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

Visual Studio emphasizes Device.OnPlatform , and when I Device.OnPlatform over a method call, I get the following warning:

Devide.OnPlatform (T, T, T) is deprecated: "Use switch (RuntimePlatform) instead.

The code originally used was from the e-book "Creating Mobile Applications Using the Xamarin.Forms Book" in 2016. I really wonder how fast this platform is evolving!

Unfortunately, I do not know how Device.OnPlatform should be replaced using the method suggested by the warning.

+8
source share
3 answers

2016 was the year this method became obsolete.

You must use the 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) } 

Of course, you can wrap this inside a function that will do the same job that you wanted to do with Device.OnPlatform, but instead of calling Device.OnPlatform you will call your own function.

+18
source
 switch (Device.RuntimePlatform) { case Device.iOS: Padding = new Thickness(5, 5, 5, 0); break; default: Padding = new Thickness(5, 5, 5, 0); break; } 
+5
source

If someone has the same problem in the XAML file, this is a way around the obsolete message:

 <ContentPage.Padding> <OnPlatform x:TypeArguments="Thickness"> <On Platform="iOs">0,20,0,0</On> </OnPlatform> </ContentPage.Padding> 
+1
source

Source: https://habr.com/ru/post/1270618/


All Articles