4

I am looking for a preprocessor symbol that would allow me to compile different code based on whether a project is xamarin or not.

void a()
{
#if XAMARIN
    b();
#else
    c();
#endif
}
3
  • __MOBILE__ is pre-defined for Xamarin iOS/Android projects: learn.microsoft.com/en-us/xamarin/cross-platform/… Dec 19, 2021 at 5:50
  • @SushiHangover I want it to work correctly with uwp as well. And these preprocessor symbols are not present in the common project.
    – weyh
    Dec 19, 2021 at 9:50
  • As Cheesebaron suggests, you will need to define your own symbols then Dec 19, 2021 at 17:04

1 Answer 1

2

I would suggest you don't use preprocessor symbols, but abstract stuff in separate classes for each platform. Then inject the specific implementation at runtime.

If you really must use these, you can always define your own symbols as needed for each configuration or target framework.

Just create a file called Directory.Build.targets (casing matters!) in the root of your repo. Usually next to your .sln file.

In this Directory.Build.targets you can define symbols like so:

<Project>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('netstandard'))">
    <DefineConstants>$(DefineConstants);NETSTANDARD;PORTABLE</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('net4'))">
    <DefineConstants>$(DefineConstants);NET;WPF;XAML</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('uap'))">
    <DefineConstants>$(DefineConstants);NETFX_CORE;XAML;WINDOWS;WINDOWS_UWP;UWP</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.ios'))">
    <DefineConstants>$(DefineConstants);MONO;UIKIT;COCOA;APPLE;IOS</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.mac'))">
    <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;MAC</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.tvos'))">
    <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;TVOS</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('xamarin.watchos'))">
    <DefineConstants>$(DefineConstants);MONO;COCOA;APPLE;WATCHOS</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('monoandroid'))">
    <DefineConstants>$(DefineConstants);MONO;ANDROID</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(TargetFramework)' == 'monoandroid10.0'">
    <DefineConstants>$(DefineConstants);MONO;ANDROID;__ANDROID_29__;</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('netcoreapp'))">
    <DefineConstants>$(DefineConstants);NETCORE;NETCOREAPP</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('netcoreapp3.'))">
    <DefineConstants>$(DefineConstants);WPF</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(TargetFramework.StartsWith('tizen'))">
    <DefineConstants>$(DefineConstants);TIZEN</DefineConstants>
  </PropertyGroup>
</Project>

Then you can use these in your project as needed:

#if PORTABLE
   // do portable stuff
#elif UWP
   // do uwp stuff
#endif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.