简体   繁体   English

使用 __ANDROID__ 在跨平台库中编写的代码不会在 Xamarin forms 应用程序中执行

[英]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.这里的符号不是预设的。

ios构建部分

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.

相关问题 用于跨平台Xamarin Forms的UserControl - UserControl for cross-platform Xamarin Forms 创建Xamarin.Forms跨平台应用程序时,“此项目需要加载Visual Studio更新”错误 - “This project requires a Visual Studio update to load” error when creating a Xamarin.Forms Cross-Platform Application 创建Xamarin.Forms跨平台应用程序时出现“此项目需要Visual Studio更新才能加载”错误 - “This project requires a Visual Studio update to load” error when creating a Xamarin.Forms Cross-Platform Application 如何在Xamarin Forms跨平台中绑定图像集合 - How to bind collection of images in Xamarin Forms Cross-Platform 无法在外部 android 设备上运行 Xamarin Forms 跨平台应用程序 - Unable to run Xamarin Forms Cross Platform Application on an external android device 用于跨平台应用程序的Python + QT,Windows Forms或Swing? - Python + QT, Windows Forms or Swing for a cross-platform application? 跨平台应用程序中的BinaryFormatter - BinaryFormatter in cross-platform application 如何重命名Xamarin跨平台应用程序? - How to rename a Xamarin Cross-Platform App? Xamarin中的跨平台OpenGL渲染 - Cross-platform OpenGL rendering in Xamarin 如何从 Xamarin Forms 在 a.xaml 文件中获取跨平台应用程序中的可用屏幕尺寸值? - How to Get The Avaible Screen Size Value In Cross-Platform App From Xamarin Forms in a .xaml File?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM