[英]Code written in Cross-platform library with __ANDROID__ does not execute in Xamarin forms application
I am using a multi-targeting library in my Xamarin forms application.我在我的 Xamarin forms 应用程序中使用多目标库。 The library has come code written with __ANDROID__
conditional compiler symbol.该库包含使用__ANDROID__
条件编译器符号编写的代码。 When i run the application on Android, it does not execute the code written with the symbol.当我在 Android 上运行应用程序时,它不会执行用符号编写的代码。
The multi-targeting library project is using MSBuild.Sdk.Extras.多目标库项目使用 MSBuild.Sdk.Extras。
<Project Sdk="MSBuild.Sdk.Extras" ToolsVersion="16.0">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;MonoAndroid90</TargetFrameworks>
</PropertyGroup>
I also tried it by defining conditional symbol in Xamarin.Android Project Properties --> Build --> General --> Conditional compilation symbols.我还通过在 Xamarin.Android 项目属性 --> 构建 --> 常规 --> 条件编译符号中定义条件符号来尝试它。 But it still does not run that particular code.但它仍然不运行该特定代码。
#if __ANDROID__
// do some andoid specific work
#endif
It should run this code block when running the application on Android.在 Android 上运行应用程序时,它应该运行此代码块。
Seems like you have defined __ANDROID__
symbol in the wrong project.好像您在错误的项目中定义__ANDROID__
符号。 If you define it in eg Project.Android
it's only defined in this project, not the other ones.如果您在例如Project.Android
中定义它,则它仅在此项目中定义,而不是在其他项目中定义。 There is a possibility to define solution level conditional symbols and you can read about it here可以定义解决方案级别的条件符号,您可以在此处阅读相关信息
Probably you forgot to add inside the 'conditional compilation symbols' field inside the build section of the Android project that symbol: ANDROID可能您忘记在 Android 项目的构建部分的“条件编译符号”字段中添加该符号: ANDROID
I don't know why on iOS when the project is created, the symbol is automatically inserted, while in the Android project it is not inserted.不知道为什么在 iOS 项目创建时,符号自动插入,而在 Android 项目中没有插入符号。
Here the symbols are not preset.这里的符号不是预设的。
Here the symbols are preset.这里的符号是预设的。
You can find more info here under Conditional Compilation section: microsoft xamarin docs您可以在条件编译部分找到更多信息: microsoft xamarin docs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.