Forum Visual Studio
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Preprocessor not switching between __ANDROID__ and __IOS__

EpicLoydEpicLoyd USMember ✭✭

Hi! I'm using shared project with forms and when i'm want to switch from *.Droid to *.iOS project for checking cross-platform code preprocessor won't undefine ANDROID and define IOS for me. Why could it be and how can i fix it?


  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited February 2017


                #region preprocessor commands __ANDROID__, __IOS__, __MOBILE__ do not work in PCL
                switch (Device.OS)
                    case TargetPlatform.Android:
                    case TargetPlatform.iOS:
                    case TargetPlatform.Windows:
                    case TargetPlatform.WinPhone:
                    case TargetPlatform.Other:
                #endregion __ANDROID__, __IOS_, __MOBILE do not work in PCL
  • EpicLoydEpicLoyd USMember ✭✭
    @ClintStLaurent I use shared project, not PCL
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Ok. Does that change the suggested solution? You can still use TargetPlatform. can't you?
    "Not working in PCL" does not imply "Does Work in Shared". What you're saying with your problem description is that it doesn't work their either.

  • EpicLoydEpicLoyd USMember ✭✭

    I can't do this because then i wouldn't be able to compile code because compiler won't compile iOS code for android and vice versa ( even if it conditioned by TargetPlatform ) so i need preprocessor working

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭


    I'm not ashamed to admit you lost me. I can't even guess what you're doing. Are you saying you have iOS native code in your PCL/Shared project and want it included based on the preprocessor directive? This whole design is starting to sound... odd... and maybe like it isn't following the paradigm of established Xamarin solutions. But I could be way off on that with so little to go on.

    Also... You know you can define any directive you want, right? If you want to define CUSTOMERRELEASE you can do that, then #if CUSTOMERRELEASE. I do this all the time so QA gets a Release build that still has some special tools to make their QA job easier, without the actual customer release having those tools/screens/shortcuts/easter-eggs. So if your issue only boil down to lack of a directive, just make new ones.

  • EpicLoydEpicLoyd USMember ✭✭

    No, i don't want to compile iOS native code, haha. I had to have sepatate code for iOS and Droid devices and i want to split it using preprocessor. Like this:

    if ANDROID

            cookie_manager.SetCookie(url, string.Format("{0}={1}", name, value));

    elif IOS

            var cookieDict = new NSMutableDictionary ();
            cookieDict.Add (NSHttpCookie.KeyOriginURL, new NSString(string));
            cookieDict.Add (NSHttpCookie.KeyName, new NSString(name));
            cookieDict.Add (NSHttpCookie.KeyValue, new NSString(value));
            cookieDict.Add (NSHttpCookie.KeyPath, new NSString("/"));
            var myCookie = new NSHttpCookie(cookieDict);

    else //WinPhone



    Also... You know you can define any directive you want, right? If you want to define CUSTOMERRELEASE you can do that, then #if CUSTOMERRELEASE.

    That is what i'm talking about: i had ANDROID and IOS defined in *.iOS and *.Droid projects. But they are changed when i'm switching from Droid to iOS project
    Here is screenshot of what i'm mean

  • EpicLoydEpicLoyd USMember ✭✭

    Oooooh, blaming myself i had not noticed it before. Thank you :>

  • AndrKrmerAndrKrmer DEMember

    @PierceBoggan said:

    This is super confusing, so I'll be filing an enhancement request. By default, this setting should default to the active startup project, rather than always defaulting to Android.

    I wondered if there are any news on that? In the current Version of Visual Studio (15.5.6) the editor view still isn't synchronized with the active startup project.

    Thanks in advance


    Microsoft MVP

  • jerryhuangjerryhuang Member ✭✭

    This problem is still persist in VS2019 16.3.7 as of today. It's not only displaying it wrong in the IDE, it also compile as what it displaying and causing the exception in runtime in my case; and cost me hours until I completely comment out the condition like below:
    //#if IOS
    // [DllImport("__Internal", EntryPoint = "decode_g726")]
    // [DllImport("libCLib", EntryPoint = "decode_g726")]
    //test below for iOS and it works
    [DllImport("__Internal", EntryPoint = "decode_g726")]

    It can be easily reproduced by creating a brand new Xamarin forms solution, and create a code shared project. The only way I'm able to get it "refresh" is to, let say, I'm setting iOS as start up project, and IOS is not working, by unloading the Android project, the IOS will then be turn on and vice versa for ANDROID. However, by reloading the Android project back in, IOS will not get turn off.....

    This will be a nightmare when it comes to production release as you won't be able to find out until run-time (at least in my case)

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @jerryhuang said:

     //#if __IOS__
     //                [DllImport("__Internal", EntryPoint = "decode_g726")]
     //        [DllImport("libCLib", EntryPoint = "decode_g726")]
     //test below for iOS and it works
             [DllImport("__Internal", EntryPoint = "decode_g726")]

    If you wrap your code in proper markdown code tags the site parser doesn't try to format your code: Turning your underscores into bold, etc.

  • DonMesserli.1843DonMesserli.1843 USMember ✭✭✭

    This is happening with Visual Studio Mac 8.4.1. Also, OnPlatform in XAML isn't working. Always picks the Android option even if the Startup Project is iOS.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Its not OnPlatform so that's probably why its not working.
    Its On Platform {With a space}

  • DonMesserli.1843DonMesserli.1843 USMember ✭✭✭
    edited January 17


    It's OnPlatform AND On Platform

    <OnPlatform x:TypeArguments="x:Double">
        <On Platform="Android">100</On>
        <On Platform="iOS">80</On>
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Ah... That's so much better when able to actually see the XAML in question.

    That's really strange though. I don't see anything wrong with your syntax. And we do a bunch of it on our app. But we develop on Windows with Visual Studio. I would have expected something as basic as On Platform to work on both IDEs.

    But that would be my first best diagnostic test to run: Compile this on Visual Studio for Windows and deploy it. If the exact same solution works there, then you know its VSMac. If it breaks in the Windows version, and it works for others in the Windows version, then its your markup.

Sign In or Register to comment.