如果当前项目是否是xamarin,如何签入c代码



我正在寻找一个预处理器符号,它可以让我根据项目是否为xamarin编译不同的代码。

void a()
{
#if XAMARIN
b();
#else
c();
#endif
}

我建议您不要使用预处理器符号,而是在每个平台的单独类中使用抽象的东西。然后在运行时注入特定的实现。

如果您真的必须使用这些符号,那么您可以根据需要为每个配置或目标框架定义自己的符号。

只需在repo的根目录中创建一个名为Directory.Build.targets(大小写很重要!(的文件。通常在.sln文件旁边。

在这个Directory.Build.targets中,你可以定义这样的符号:

<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>

然后你可以根据需要在你的项目中使用这些:

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

最新更新