android - 跨平台 xamarin 应用程序和 native 库绑定(bind)

标签 android ios xamarin.ios cross-platform xamarin

我目前有一个原生的 Android 和 iOS 库,其中包含由模型(最小部分)、 View 和 Controller (最大部分)组成的不同“模块”。

我想开始使用 xamarin 编写应用程序,而不会丢失我在此库中所做的所有工作。但似乎 native 库绑定(bind)更适合实用程序库,或仅包含 MVC 组件的 1 部分而不是相互依赖的 3 部分的库。

所以在阅读了关于跨平台开发和本地库绑定(bind)的 Xamarin 文档之后,我认为我应该使用 Xamarin C# 库重写我的库的所有模型部分,我会在 iOS 上放弃 Core Data,在 Android 上放弃 ORM Lite , 而我会使用提供的 SQLite 库。这项工作听起来并不大,所以这不是问题(从长远来看,它会减少维护工作量)。但是现在问题来了,当我的模型将是 C# 类时,我将如何在仍然使用 Obj-C 和 Java 编写的 Controller 中使用这些模型?我能否从可在 iOS/Android 代码中导入的 C# 模型类中获取头文件?或者我是否还需要使用 C# 重写 Controller (这对我来说是不适合使用 Xamarin 的)?

我愿意接受任何可以避免失去数月工作的迁移建议/策略,所以如果您有的话,请随时回答 :)

最佳答案

I currently have a native Android AND iOS library that contains different "modules" composed of models (the smallest part), views and controllers (the biggest part).

这可能意味着您当前有两个 库,一个用于 iOS,一个用于 Android。一个用 Objective-C 编写,一个用 Java 编写。

I would like to start writing apps using xamarin without losing all the work I did on this library. But it seems like if the native library binding is more suited for utilities libraries, or libraries that only contains 1 part of the MVC components, not the 3 which are depending from each other.

是什么让你这么想的?你可以绑定(bind)任何你想绑定(bind)的东西。而且您不需要一开始就绑定(bind)所有内容。如果现在您只想绑定(bind)处理 UI 的 native 库中的某些部分,请跳过其余部分。您可以稍后添加绑定(bind)。

I think that I should rewrite all the model part of my library using a Xamarin C# library, I would drop Core Data on iOS, and ORM Lite on Android, and instead I would use the SQLite library provided.

这是一个有效的方法。但是,您将无法继续使用您的 native 代码。没有办法从 ObjC 到 C#。反之亦然。即使有可能:您也必须更改您的 Controller 以与 SQLite.Net 而不是 CoreData 通信,或者添加一个抽象层,而这反过来又必须在 C# 端进行绑定(bind)和实现。与坚持使用 native 版本或用 C# 重写所有内容相比,所有这一切可能需要更多工作。

Can I obtain header files from the C# models classes that I can import in the iOS / Android code ? No, you cannot.

Or do I also need to rewrite the controllers using C# (that would be a no-go to using Xamarin for me) ? You have the choice:

  • 坚持你所拥有的,忍受维护两个库(和两个应用程序代码库)的局限性
  • 绑定(bind)库并围绕它们创建托管抽象。然后使用来自 PCL 的抽象并从 Xamarin C# 和 Android 应用程序引用该 PCL。接受这个解决方案,直到您对维护两个代码库感到不满意,然后:
  • 重写您的 native 代码并切换到 Xamarin 并拥有一个代码库。

也许还有关于绑定(bind)的注意事项:绑定(bind)并不意味着交换 native 功能的一部分。它们将 native 功能传输到托管世界。它们无法让您选择两全其美,然后创建一个万能的解决方案。 如果你留在本地世界,你最终会得到相同的结果:想一想如果你想从 iOS 上的 CoreData 更改为基于 Json 的存储,你必须做什么?除非您准备好一切以支持数据层的抽象,否则您将不得不接触所有 Controller 。

关于android - 跨平台 xamarin 应用程序和 native 库绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21666451/

相关文章:

ios - AVAudioEngine schedule sample 准确的参数变化

ios - 删除使用 NSMutableAttributedString 的 UILabel 空白区域的彩色背景

ios - 如何解析我的 CSV 文件?

c# - 如何在 MonoTouch 和 Objective-C 之间做出选择?

java - 如何让我的 ArrayAdapter 遵循 ViewHolder 模式?

android - 路线不会在带有 "Google-Directions-Android"库的 map 上绘制

android - meteor 热码推送的Layout/Template

Android 值-小 不使用

c# - 如何在 Xamarin 中设置 NSDictionary<NSString, NSObject> 对象的值?

android - iOS 和 Android 应用程序的图像分辨率