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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With