Why use Xamarin for cross-platform development?
Using Xamarin for cross-platform application development saves valuable time. The mobile application development team will be able to easily generate it for iOS, Android, and Windows Phone thanks to the Xamarin.Forms tool. In a few words, Xamarin.Forms is the tool that allows developers to generate iOS and Android applications using a single codebase.
Xamarin also offers a comprehensive library (Xamarin.Essentials) of iOS and Android features (e.g., geolocation, camera, etc.) and their implementation in the developer's code is very simple.
Adaptation to different screen types is also quickly achieved thanks to XAML. Switching from a desktop version to a mobile version is therefore simplified.
The Framework is suitable for all types of applications, whether small or more substantial. However, it will be necessary to ensure that the project structure and architecture are adapted.
The + Thanks to Xamarin and its Microsoft technologies, it's easy for a developer .NET to learn and/or integrate into an ongoing project. It is then possible to reuse existing resources and skills.
Xamarin applications can be architected in a few primary ways, depending on the project's needs and the level of code sharing desired. The most common architectural approaches are: 1. **Xamarin.Android and Xamarin.iOS (Native UI)** * **Description:** This is the most straightforward approach. You create separate projects for Xamarin.Android and Xamarin.iOS. Each project has its own UI layer written using the native Android (XML layouts, Activities) and iOS (storyboards, ViewControllers) UI toolkits. Business logic, data access, and shared components are implemented in a shared Portable Class Library (PCL) or a .NET Standard library. * **UI:** Native UI elements for each platform. * **Code Sharing:** Primarily business logic, data, and services. UI code is platform-specific. * **Pros:** Leverages native look and feel, maximum performance and access to platform features. Developers can reuse existing native UI skills. * **Cons:** Requires maintaining two separate UI codebases. 2. **Xamarin.Forms** * **Description:** Xamarin.Forms allows you to build a single UI codebase that is shared across Android, iOS, and UWP (and other platforms). You define your UI using XAML or C# code, and Xamarin.Forms renders the native UI controls on each platform. * **UI:** Abstracted UI elements that are mapped to native controls at runtime. * **Code Sharing:** UI, business logic, data access, and services are all shared. * **Pros:** Maximum code reuse, faster development time for cross-platform applications with similar UI needs, easier to maintain a consistent look and feel. * **Cons:** Can sometimes have a slight performance overhead compared to native UI. Customizing the UI to perfectly match native platform conventions can sometimes be challenging or require platform-specific renderers. 3. **Xamarin.Essentials** * **Description:** While not an architecture in itself, Xamarin.Essentials is a crucial component for both Xamarin.Android/iOS and Xamarin.Forms. It provides a common, cross-platform API set for accessing native device features such as battery, network, sensors, file system, and more. This significantly reduces the need for platform-specific code for common device integrations. **Key Architectural Patterns Used Within These Approaches:** Regardless of the chosen primary architecture (Native UI vs. Forms), common design patterns are employed to structure the application effectively: * **MVVM (Model-View-ViewModel):** This is the most popular pattern with Xamarin.Forms and also very common with Xamarin.Android/iOS. * **Model:** Represents the data and business logic. * **View:** The UI elements (XAML oder native UI). It's passive and designed to bind to the ViewModel. * **ViewModel:** Acts as an intermediary between the Model and the View. It exposes data and commands that the View can bind to, and handles user interactions. It's responsible for preparing data for the View and managing the state of the View. This pattern facilitates testability and separation of concerns. * **MVC (Model-View-Controller):** More commonly seen in traditional native Android and iOS development, it can also be applied in Xamarin.Android and Xamarin.iOS projects. * **Model:** Data and business logic. * **View:** The UI. * **Controller:** Handles user input and updates the Model and View. * **Dependency Injection (DI):** A design pattern that allows objects to receive other objects they depend on. In Xamarin, DI frameworks like Autofac, StructureMap, or even simple manual DI can be used to manage dependencies, making the code more modular, testable, and maintainable. * **Repository Pattern:** Used for abstracting data access logic. A repository handles communication with the data source (e.g., local database, remote API) and provides a clean interface for the ViewModel or Controller to fetch and save data. **In summary, a Xamarin application's architecture typically involves:** * **A shared core:** Often a PCL or .NET Standard library containing business logic, data models, and potentially shared services. * **A platform-specific UI layer:** Either native Android/iOS UI or a cross-platform UI defined in Xamarin.Forms. * **Xamarin.Essentials:** For accessing native device functionalities. * **Design patterns like MVVM or MVC:** To structure the code logically and promote maintainability. * **Dependency Injection and Repository Patterns:** For better code organization and testability.
- Choice of platform on which to develop the project: iOS / Android / Universal Windows Platform.
- Choosing an architecture (pattern) such as MVVM (Model-View-ViewModel) to facilitate testing.
- Development of the shared project in .NET
- Development of graphical interfaces (UI) for different platforms in the shared project using Xamarin.Forms.
- Development of logical and graphical feature exceptions on iOS or Android projects. For example: modifying a default visual component (e.g., the timepicker) of Xamarin.Forms or specifying different logical behaviors based on the target platform.
See the illustration above (source: www.medium.com)
Applications developed in Xamarin at Technology Partner
The Technology Partner teams have already developed several applications in Xamarin, including these two projects:
- A worldwide helicopter booking application.
- A private jet booking application worldwide for the company LXA FlyerApp
(View project sheet) - An application currently under development for a department of the Luxembourg Ministry (project sheet to follow)
Written by Aurélien Kauten & Charline Pennisi