Alan2
Alan2

Reputation: 24592

Is there any new way of doing an OnPlatform check in Xamarin Forms code behind?

I have this Xaml

Margin="{OnPlatform Android='20,0,20,0', iOS='20,0,20,30'}"

I am changing the Xaml to C#

Is there any better option now than using something like this:

switch(Device.RuntimePlatform)

Upvotes: 2

Views: 1029

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 34083

According to the Docs there is not. You will still have to use something like this:

double top;
switch (Device.RuntimePlatform)
{
  case Device.iOS:
    top = 20;
    break;
  case Device.Android:
  case Device.UWP:
  default:
    top = 0;
    break;
}
layout.Margin = new Thickness(5, top, 5, 0);

There is the C# Markup now that might make the syntax a bit easier? Note that that functionality moved/is moving to the Xamarin Community Toolkit

Upvotes: 1

Related Questions