In Xamarin.Forms, what is the common way to branch between IOS and ANDROID methods?


I am new to Xamarin.Forms. I think like a normal coder. If Android, do this… if Ios, do this…

In Xamarin Forms, say someone clicks a button which is binded to a view model, what is the common way to select a platform specific method between Android and IOS?

FYI, there are 3 projects in a Xamarin Solution

  • Project
  • Project.Android
  • Project.IOS

The View model is inside ‘Project’ >

 `private async void OnLoginClicked(object obj)

           //If Android??

           //If IOS?


You can use Device class for this purpose. It has property RuntimePlatform to check current platform.

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

More info :

Answered By – Qwerty

Leave a Comment