Home > Enterprise >  Where Are Preprocessor Directives Set in MAUI Projects
Where Are Preprocessor Directives Set in MAUI Projects

Time:08-24

I have a MAUI project that I built on a Windows machine for iOS and Android devices. Everything works fine there.

When I try and run the same code on Visual Studio for Mac, it fails because it can't find code for iOS devices that are defined within a preprocessor directive, like this:

#if ANDROID
//do some Android stuff
#elif IOS
//define some properties
#endif

It won't compile and run because it says it can't find the properties that are defined in the "IOS" block. It shows the code in the "ANDROID" preprocessor directive block as active. On Windows, it automatically selects the correct directive to use based on which debugger output you select (like Android emulator or device, iOS emulator or device). It's not doing this on the Mac, so is there some way to force it in VS 2022 17.3 for the Mac?

CodePudding user response:

I finally figured this out...just one of those things that's hard to find on the Mac if you're used to Windows I guess. Here's a screenshot:

enter image description here

The red circle is the build configuration, and next to it is the target. In Windows when you select the target it automatically flips the target OS as well. On the Mac, it doesn't happen. So the pink circle is around a dropdown where you manually select the target OS, and when you do that the OS-appropriate compiler preprocessor options will be enabled accordingly. You can see in the screenshot below that now the code in the "IOS" block is finally active.

  • Related