Does Xamarin have an #if or #ifdef for determining the platform?

10,867

Solution 1

iOS:

#if __MOBILE__
    Console.WriteLine ("__MOBILE__ is defined");
#endif 

#if __IOS__
    Console.WriteLine ("__IOS__ is defined");
#endif 

Android:

#if __MOBILE__
    Console.WriteLine ("__MOBILE__ is defined");
#endif 

#if __ANDROID__
    Console.WriteLine ("__ANDROID__ is defined");
#endif

https://bugzilla.xamarin.com/show_bug.cgi?id=6459#c12

xamarin documentation

Solution 2

Yes it does, I don't know, if Xamarin.iOS provides its own symbols, as I'm new to Xamarin and I actually do not use Xamarin.iOS, but you can define your own symbols.

Right click on the project an open the project options. In the "Compiler" settings you can lookup existing flags and create add new ones. For example here are the symbols that are shipped with Xamarin.Android:

DEBUG;__MOBILE__;__ANDROID__;

The flags should be available immediately after you have defined them.

Share:
10,867
William Jockusch
Author by

William Jockusch

Free Graphing Calculator -- iOS: https://itunes.apple.com/us/app/free-graphing-calculator/id378009553?mt=8 Android: https://play.google.com/store/apps/details?id=com.jockusch.freegraphingcalculator Mac: https://itunes.apple.com/us/app/free-graphing-calculator-2/ Windows: https://www.microsoft.com/store/apps/9pgllk5gj04h

Updated on July 17, 2022

Comments

  • William Jockusch
    William Jockusch almost 2 years

    For example, #ifdef iOS, #ifdef android, or the like. If there is #if, that would be even better.

  • amenthes
    amenthes about 9 years
    It is worth noting that they are defined in the respective .csproj files. Look for the <DefineConstants> tag in your subproject. This can be seen in this seach within Xamarin Forms Samples.
  • amenthes
    amenthes about 9 years
    Also this piece of Documentation is quite good: developer.xamarin.com/guides/cross-platform/…