Code sharing between MonoTouch and MonoForAndroid

為{幸葍}努か 提交于 2019-11-28 19:01:47

This is a very general question, but here are a few resources that may help you get started:

  1. Video: Cross-platform Mobile Development
  2. Blog Post: Shared Libraries For Windows Phone 7, MonoDroid and Beyond
  3. Book: Mobile Development with C#
  4. Blog Post: Xamarin Mobile World Congress 2012 Unofficial Conference App Released!

Edit (to answer your new questions)

  1. The idea behind linking files across projects is that there is only one actual copy of the file, rather than having to manage multiple copies and keep them in sync yourself. The file will actually exist in just one project and be linked into the others, but when the projects are compiled it treats the file as if it were actually there.

  2. I can't speak to exactly how they created their folder structure, but I know there have been many cases where I would manually edit project or solution files to get the folder structure I want, because there was no way to get what I wanted through the IDE alone. This really boils down to personal preference on how you want your folders to be structured.

  3. In the end, what you need is a class library project for every platform you want to target. When going with the linked file approach, it's totally up to you where you put the physical files. One approach I use often is to actually create a standard .NET 4.0 class library, put the files in there, and then link them into my Mono for Android and MonoTouch class libraries. If all you care about is targeting iOS and Android, that may be more trouble than it's worth, and you can just let the files live in one project and link them into the other.

Disclaimer: I've got a particular Mvvm methodology that I use for sharing code across multi-platform projects...

Despite this, I genuinely don't believe in "one size fits all" frameworks - I think you need to be careful to pick an approach that best suits your project, your developers and your organisation.

With that said, some of the tools you can use within the Mono development approach are:

  • using Portable Class Libraries to share exactly the same code between platforms
  • using platform specific Class Libraries to share code between platforms, linking these using the Project Linker tool from Microsoft
  • using #define code within your class libraries to provide platform specific implementations of the projects (I personally try to avoid this approach, but it does often provide the quickest route to market)
  • using DI/IoC techniques to provide components for those occasions when platform specific implementations really are required.
  • using a assembly linking to provide IoC - e.g. this is what the Xamarin MobileAPI does
  • using server-based logic for genuine shared functionality - e.g. using REST or SOAP-XML services to implement logic
  • sharing tests (e.g. NUnit) between platforms to assure the quality of your logic
  • using shared code techniques - MVC (MonoCross) or MVVM (MonoMobile.Views or MvvmCross) for UI "controller" logic; MonoTouch.Dialog and MonoDroid.Dialog for "View-level" abstractions; CrossGraphics for UI "drawing"; SQLite.Net for database; etc.

I'm finding the MonoTouch, MonoDroid and the Microsoft tools provide real and signigicant benefits in developing cross platform code - but you do have to work and think to achieve this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!