Building Cross Platform Applications Overview
This guide introduces the Xamarin platform and how to architect a cross-platform application to maximize code re-use and deliver a high-quality native experience on all of the main mobile platforms: iOS, Android and Windows Phone.
The approach used in this document is generally applicable to both productivity apps and game apps, however the focus is on productivity and utility (non-game applications). See the Introduction to MonoGame document or check out Visual Studio Tools for Unity for cross-platform game development guidance.
The phrase “write-once, run everywhere” is often used to extol the virtues of a single codebase that runs unmodified on multiple platforms. While it has the benefit of code re-use, that approach often leads to applications that have a lowest-common-denominator feature-set and a generic-looking user interface that does not fit nicely into any of the target platforms.
Xamarin is not just a “write-once, run everywhere” platform, because one of its strengths is the ability to implement native user interfaces specifically for each platform. However, with thoughtful design it’s still possible to share most of the non-user interface code and get the best of both worlds: write your data storage and business logic code once, and present native UIs on each platform. This document discusses a general architectural approach to achieve this goal.
Here is a summary of the key points for creating Xamarin cross-platform apps:
- Use C# - Write your apps in C#. Existing code written in C# can be ported to iOS and Android using Xamarin very easily, and obviously used in Windows apps.
- Utilize MVC or MVVM design patterns - Develop your application’s User Interface using the Model/View/Controller pattern. Architect your application using a Model/View/Controller approach or a Model/View/ViewModel approach where there is a clear separation between the “Model” and the rest. Determine which parts of your application will be using native user interface elements of each platform (iOS, Android, Windows, Mac) and use this as a guideline to split your application into two components: “Core” and “User-Interface”.
- Build native UIs - Each OS-specific application provides a different user-interface layer (implemented in C# with the assistance of native UI design tools):
- On iOS, use the UIKit APIs to create native-looking applications, optionally utilizing Xamarin's iOS designer to create your UI visually.
- On Android, use Android.Views to create native-looking applications, taking advantage of Xamarin’s UI designer.
- On Windows you will be using XAML for presentation layer, created in Visual Studio or Blend’s UI designer.
- On Mac, you will use Storyboards for the presentation layer, created in Xcode.
Xamarin.Forms projects are supported on all platforms, and allow you create user interfaces that can be shared across platforms using Xamarin.Forms XAML.
The amount of code re-use will depend largely on how much code is kept in the shared core and how much code is user-interface specific. The core code is anything that does not interact directly with the user, but instead provides services for parts of the application that will collect and display this information.
To increase the amount of code re-use, you can adopt cross-platform components that provide common services across all these systems such as:
- SQLite-net for local SQL storage,
- Xamarin Plugins for accessing device-specific capabilities including the camera, contacts and geolocation,
- NuGet packages that are compatible with Xamarin projects, such as Json.NET,
- Using .NET framework features for networking, web services, IO and more.
Some of these components are implemented in the Tasky case study.
Separate Reusable Code into a Core Library
By following the principle of separation of responsibility by layering your application architecture and then moving core functionality that is platform agnostic into a reusable core library, you can maximize code sharing across platforms, as the figure below illustrates:
There is one case study that accompanies this document – Tasky Pro. Each case study discusses the implementation of the concepts outlined in this document in a real-world example. The code is open source and available on github.