# Xamarin Forms

## Summary


In traditional app development process, developers had to write code for iOS, Android and Windows platform separately. They had to maintain the codebase of each platform for the maintenance and upgradation process. It was a heavy time and money consuming task. Xamarin solved this problem to a great extent by providing the facility of backend code sharing across the platforms. It allowed developers to create the business logic once and share it across the iOS, Android and Windows platform. Eventually, Xamarin.Forms was developed to allow the developers to share the code of user interface of app across the platforms and make the development process easier.

Xamarin.Forms is an open source cross-platform UI development framework. It allows developers to build Android, iOS and Windows application from a single shared codebase. It supports C# language. Visual Studio is the recommended IDE for Xamarin.Forms.

## Discussion

### How does Xamarin.Forms work?

A Xamarin.Forms application is designed similarly as a traditional cross-platform application. Shared code is usually placed in a .NET Standard library and platform-specific applications consume the shared code. 

Xamarin.Forms applications have a single class named App that instantiates the application on each platform. The *AppShell* class defines the visual hierarchy of the application. Shell uses this visual hierarchy to produce the user interface of the app. 

At runtime, Xamarin.Forms uses platform renderers to convert the cross-platform UI elements into native controls on Android, iOS and Windows devices. It enables the developer to give the native look, feel and user experience to the application while using the feature of code-sharing across platforms. 


### What are the benefits of Xamarin.Forms?

It allows developers to create native UI layout and designs across platforms using a single shared codebase which reduces the time and money deployed in the development process . It allows developers to share code, test and business logic of app across multiple platforms. It reduces the overhead of managing different codebases for app due to separate code for different platforms.

It's been claimed that while Xamarin enables 70% code sharing, Xamarin.Forms allows up to 95% code sharing. It's best for apps where code sharing is more important than custom UI. One team reported that with Xamarin.Forms they had only 135 lines of platform-specific code out of 3744 lines (96% reuse). Earlier with Xamarin the team had achieved 80% code reuse. 

PGS Software implemented European airline Volotea's mobile app in Xamarin.Forms. With Forms, consistent branding and styling were achieved across iOS and Android. With Forms, 70% of custom renderers were removed. 


### What platforms are supported in Xamarin Forms?

Xamarin.Forms is used for making mobile apps (iOS or Android) and Universal Windows Platform (UWP) apps. UWP enables creation of apps that can run on any device with Windows 10 or 11. Visual Studio is the recommended IDE for it. For iOS, Xamarin.Forms applications can be written for iOS 9 or higher versions. For Android, apps can be written for Android 4.4 (API 19) or higher but Android 5.0 (API 21) is recommended as minimum API for full compatibility with all the Android supported libraries. For Windows platform, Windows 10 Universal Windows Platform, build 10.0.16299.0 or greater for .NET Standard 2.0 support. However, build 10.0.18362.0 or greater is recommended. 

Apart from iOS, Android and Windows Xamarin.Forms supports platforms including Samsung Tizen, macOS 10.13 or higher, GTK# and WPF. Windows 8.1 / Windows Phone 8.1 WinRT and Windows Phone 8 Silverlight are deprecated platforms for Xamarin.Forms version 3.0 or newer. 

To Develop for iOS platform on Windows computer, there must be a Mac computer accessible on the network because Apple's build tools are required to build native iOS apps, which run on Mac only. So, Visual Studio should be connected to a network-accessible Mac. 


### What are some essential terms used in Xamarin.Forms?

Below are some essential terms used in Xamarin.Forms-

    + **Views**: Views are the basic building blocks of the user interface. Labels, buttons, and sliders are different types of views. Different views serve different purposes such as setting values and editing text.
    + **Layouts**: Layouts are the visual structure of the user interface. Layouts are containers for views and other layouts. Layouts and views derive from the *View* class. Layouts are also views containing a collection of child views.
    + **Effects**: Effects are used to customize the controls accordingly without implementing custom Renderer classes. They should be used when a small styling change is required. While, Custom renderers are better option for creating a new control.
    + **Gestures**: Gesture recognizers are used to detect user interaction with views in a Xamarin.Forms application. The Xamarin.Forms GestureRecognizer class supports tap, pinch, pan, swipe, and drag and drop gestures on View instances of the app.
    + **Triggers**: Triggers are used to define the actions in XAML after a certain events or property changes. State triggers are a specialized group of triggers that define when a VisualState should be applied.

### What are the key building blocks of Xamarin.Forms?

The key building blocks of a Xamarin.Forms application are as follows:

    + **Xamarin.Essentials**: Essentials provides a single cross-platform API that can work with any Xamarin.Forms, Android, iOS, or UWP application accessible from shared code regardless of how the user interface is created.
    + **eXtensible Application Markup Language (XAML)**: XAML is used for instantiating and initializing objects and organizing those objects in a hierarchical way. In XAML, developers can use all the Xamarin.Forms views, layouts, pages as well as custom classes to create the UI. It is very brief and readable than the equivalent code.
    + **Behaviors**: Behaviors allows to add functionality to the user interface controls without extending to any other class. The functionality is already implemented in the behavior class so developers only need to attach it to the controls. They are created by deriving from the Behavior or Behavior<T> class.
    + **Data binding**: It is used to keep the objects synchronized so that changes in one object's property are automatically reflected in other related object's property. It is an important part of the Model-View-ViewModel (MVVM) application architecture.

### What are the code-sharing techniques in Xamarin Forms?

The.NET Standard Libraries and Shared Projects are two main methods of code-sharing. Developers can use a large number of .NET APIs to share the code across multiple platforms. .NET Standard 1.0 - 1.6 provides larger sets of APIs, whereas .NET Standard 2.0 provides the best coverage of the .NET Base Class Libraries (including the .NET APIs available in Xamarin apps). . In the shared projects method, a project is shared containing all the code files and assets of the app. Separate application projects for iOS, Android and Windows are shared inside the shared project. The common code for all the platforms also resides in the shared project. 

Portable Class Libraries allow developers to choose the combination of platforms for their project. However, Portable Class Libraries are deprecated in the latest version of Visual Studio, and .NET Standard Libraries are recommended instead. 


### What are platform-specific features and how to create them?

A platform may have features and functionalities that doesn't exist on other platforms. For instance, Android has a functionality of fast scrolling in a list view but iOS doesn't have the same. Xamarin.Forms platform-specifics allow developers to utilize such platform-specific functionality that is only available on a specific platform without creating custom renderers or effects. There are some built-in platform-specifics in Xamarin.Forms, developers can use them or create the platform-specifics by themselves. Below are the steps for creating a platform-specific -

    + Implement the specific functionality as an Effect.
    + Create a platform-specific class that will use the Effect.
    + Make an attached property in the platform-specific class to allow the platform-specific to be used through XAML.
    + Implement extension methods in the platform-specific class, to allow the platform-specific to be used through a fluent code API.
    + Set the effect implementation to be applied only when the platform-specific has been invoked on the same platform as the Effect.Using an Effect as a platform-specific makes the effect easily consumable through XAML and a fluent code API. 


### What are the shortcomings of Xamarin forms?

Though Xamarin.Forms can create a native UI for different platforms but it is not the best choice for creating native UI of complicated apps or apps that use more native functionalities such as camera, bluetooth.  Also, the file size of Xamarin.Forms app is comparatively greater than the native ones. It is not a good choice for building games.


### As a beginner, how do I get started with Xamarin.Forms?

Xamarin.Forms has a complete documentation and code samples. There are many resources to learn Xamarin.Forms provided by the Microsoft, including:

    + Complete official documentation
    + Xamarin.Forms book
    + Xamarin.Forms tutorials
    + Code samples
    + Question/Answer communityA beginner should use Xamarin.Forms Shell for the mobile app development. Xamarin.Forms Shell provides a common navigation user experience, a URI-based navigation scheme, and an integrated search handler. Thus, it makes the app development process easier. 

## Milestones

2011

Engineers who had created Mono, launch a new company named **Xamarin**. Mono itself was an open source project started in 2001 to bring .NET applications to Linux. 

2014

Xamarin.Forms is created, enabling a single user interface to be used for different platforms. **Xamarin.Forms 1.0.6197** is released as a bug fix release.

Apr  
2018

**Xamarin.Forms 2.5.1** is released. Multiple issues such as BindingContext object not being passed through CommandParameter and app crash while navigating to a TabbedPage are solved. 

May  
2018

**Xamarin.Forms 3.0.0** is released. Flex layout is released. It's optimized for UI to adapt to various screen sizes and dimensions. Support for right to left languages is released. Developers can now create and customize Styles using CSS. VisualStateManager can now be used to handle different states of controls. 

May  
2020

At the BUILD 2020 conference, Microsoft announces **.NET Multi-platform App UI (MAUI)**. This is meant as an evolution of Xamarin.Forms. Xamarin.Forms 5.0 will be the last major release before .NET MAUI. 

Nov  
2020

**Xamarin.Forms 4.5.0.725** (4.5.0 Service Release 6) is released. Xamarin.Forms now uses the latest AndroidX libraries from Google instead of Android Support libraries. 

Jul  
2021

**Xamarin.Forms 5.0.0.2337** (5.0.0 Service Release 9) is released. The release includes several new controls and features including App Themes (Dark Mode), Brushes, CarouselView, RadioButton, Shapes and Paths, and SwipeView. Visual Studio 2017 is no longer supported.