Forum Cross Platform with Xamarin

VS2017, Conditional Compilation for platforms not working

Hello Forum!
I am developing cross platform applications in VS2017. Recently I installed the latest VS and Android SDK Manager Updates and SDK Platforms.

VS: 15.5.5
Xamarin Android SDK: 8.1.5.0
Visual Studio Tools for Xamarin: 4.8.0.760
Android SDK Tools: 25.2.5
Android SDK Platform-tools: 27.0.1
Android SDK Build-tools: 27.0.3

Besides several problems with the emulators and nuget packages that I solved more or less, I do not get the platform conditions to work as they did before.

    #if __IOS__
        int i;
    #elif __ANDROID__
        int i;
    #else
        int i;
    #endif

I use these macros a lot in my shared lib to handle the platform specific stuff. VS opens the project and activates IOS for example. When I switch between the startup projects or platform... nothing happens. It remains IOS no matter what I do. The only workaround I discovered by now is unloading the platform projects that I don't need.

I did a repair installation of my VS, reset settings, tried different Android target version in the project settings, set up new projects to test if they behave the same. Nothing worked.

Any idea?

Posts

  • FishDevFishDev USMember ✭✭

    @henne
    I've never used conditional compilation personally, but if you look at the bottom of https://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/part_4_-_platform_divergence_abstraction_divergent_implementation/ I don't see it mention #elif anywhere. Have you tried something more like.

    #if __IOS__
        int i;
    #else
    #if __ANDROID__
        int i;
    #else
        int i;
    #endif
    #endif
    
  • hennehenne Member ✭✭

    Here are some infos regarding this topic, that I gathered within the last months of developing with Xamarin.Forms:

    The combination #if , #elif, #else works fine with the macros.

    Watch out for the drop-down box in the upper left corner directly above the code editor control. It allows switching between the start projects, that reference the active shared project. I noticed this box way too late, but it solved my problem.

    It is possible to add more than one shared library (with the advantage of the macros) to your Xamarin.Forms cross-platform project. This may be obvious for many of you, but for me as a dev. with a german UI Visual Studio, it is beyond my understanding that
    1. A Shared Library is translated as "Freigegebenes Projekt", which means something like approved or unlocked project.. and
    2. The Shared Library is subordinated to "Windows Desktop" in the "Add to project" dialog, which makes no sense at all in the context of XF cross-platform projects.

Sign In or Register to comment.