-1

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 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?
        }`
user1034912
  • 1,858
  • 4
  • 32
  • 45
  • 1
    Does this answer your question? [Write device platform specific code in Xamarin.Forms](https://stackoverflow.com/questions/24251020/write-device-platform-specific-code-in-xamarin-forms) – ToolmakerSteve Apr 06 '21 at 00:12
  • Hell no.. i was looking for simple if statements! – user1034912 Apr 06 '21 at 01:35
  • I understand. That is shown in [this answer](https://stackoverflow.com/a/62293993/199364) of that link. Otherwise, I would not have suggested that this was a duplicate of that question. If you upvote that answer there, this will help others find that solution, in one central Q&A with all approaches. – ToolmakerSteve Apr 06 '21 at 18:42

1 Answers1

1

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;
    break;
  case Device.Android:
  case Device.UWP:
  default:
    top = 0;
    break;
}
layout.Margin = new Thickness(5, top, 5, 0);

More info : https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/device

Qwerty
  • 71
  • 7
  • Thanks, is this how everyone else does it? Seems like there are # directives like this => https://docs.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/building-cross-platform-applications/platform-divergence-abstraction-divergent-implementation – user1034912 Apr 05 '21 at 07:40
  • Have a look here : https://forums.xamarin.com/discussion/182089/xamarin-forms-conditional-compilation-not-working – Qwerty Apr 05 '21 at 09:42
  • 1
    If you want to use pre-processor constants conditions you needs to make some changes on your .csproj file https://stackoverflow.com/a/65645593/5228202 – Cfun Apr 05 '21 at 10:05
  • Thanks for answering! To future readers: In the interests of maintaining one Q&A showing all solutions, be aware that there is an earlier Q&A, in which [this answer](https://stackoverflow.com/a/62293993/199364) is similar to the answer here. If you find this answer useful, please upvote that other one also, so that it will be easier for people to find. Thanks. – ToolmakerSteve Apr 06 '21 at 18:49
  • @user1034912 - the disadvantage of using preprocessor directives, is it means the common project has to be recompiled for each platform. That means you can't do one build that simultaneously prepares all targets. And you have to be careful when you switch from one target platform to the other - must make sure that the common project gets rebuilt, even if nothing has changed. – ToolmakerSteve Apr 06 '21 at 18:56