locked
Xamarin.Forms - conditional compilation not working RRS feed

  • Question

  • User258412 posted

    My App1 is a Xamarin Forms solution. * In App1.Android no compilation directive is defined. * In App1.UWP the WINDOWS_UWP directive is defined. * In App1 (shared code) I am trying to execute platform specific code using:

        #if __ANDROID__
                      // Android-specific cod
        #elif WINDOWS_UWP
                    // UWP-specific code
        #else
                    // Other;
        #endif
    

    ..but compiler fails to recognise the directives whatever platform I choose for compilation... I also tried to add a specific extra Shared Project but it gave the same result....

    What is wrong, please help

    Monday, May 25, 2020 3:35 PM

Answers

  • User369979 posted

    No, it isn't. The documentation is talking about the Shared Project: It consumes the conditional symbols of all referenced projects. And it could consume native code directly. But .Net Standard Forms project is different. We use the dependency service to call native code on each platform as I said above.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, May 28, 2020 2:28 AM

All replies

  • User369979 posted

    You define this conditional symbol on each separate project but it doesn't work on Forms project. Forms has its own conditional compilation symbols. Try to right-click the Forms project, you will see it in the build section. If you want to trigger platform-specific code try this code:

    if (Device.RuntimePlatform == Device.Android)
    {
    
    }
    else if (Device.RuntimePlatform == Device.UWP)
    {
    
    }
    else
    {
    
    }
    
    Tuesday, May 26, 2020 3:49 AM
  • User258412 posted

    Thanks, yes this works for conditional execution of code in runtime but I believe is not the same as during compilation..

    I was trying to follow the instructions given here https://docs.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/building-cross-platform-applications/platform-divergence-abstraction-divergent-implementation

    My cross platform project is about using the USB-serial-port, retrieve some data from external hardware and present in UI. I was planning to reuse 100% of the code for UI and business logic but for the USB-port I need to access platform specific namespaces (API:s) for UWP, Android etc.

    Maybe I missed something here, but how could this be achieved if not by conditional compiling..? Are you saying this is not suppose to work at all for Forms, if so when/how could it be used, any example?

    Tuesday, May 26, 2020 7:32 AM
  • User369979 posted

    The scenario you described is much like a PCL project. But we now all use .Net Standard Forms project to establish cross-platform applications. We can't use native iOS or Android code directly in Forms project. However, dependency service: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction helps us achieve this. We even don't need to know which platform the application is running on. We only need to call the dependency service to help us consume different codes on different specific platforms.

    Wednesday, May 27, 2020 8:39 AM
  • User258412 posted

    Thanks, this might do the trick!

    Last question: does it mean the conditional compiling is absolute..?

    Wednesday, May 27, 2020 6:36 PM
  • User369979 posted

    No, it isn't. The documentation is talking about the Shared Project: It consumes the conditional symbols of all referenced projects. And it could consume native code directly. But .Net Standard Forms project is different. We use the dependency service to call native code on each platform as I said above.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, May 28, 2020 2:28 AM