Platform Independent Apps: Xamarin Forms

Xamarin Forms
Feb 15, 2021

A need for Xamarin.Forms

In today’s world, where technology is rapidly moving and everybody is becoming dependent upon technological advancement because of the ease it gives us in every manner, there is a need of making this ease available to everyone and everywhere which brings us to a position where developers need to create mobile applications of all types to cater all the real-world ease through technology to the common man and when we think about developing a mobile application then there comes the fact that we have multiple platforms for it, mainly Android and iOS.

If a developer thinks of developing an application for both platforms then they must initiate by creating apps for either platform separately, and this becomes a nightmare because installing multiple software at once and then setting up an environment for it and after all this must learn different languages for different platforms and their norms.

Here comes the fact that there is a framework that offers you a delightful option of developing applications for multiple platforms with a single code base. And that is Xamarin.Forms, yes you heard it right, Xamarin.Forms offer developers a framework where one can develop applications with a single code base, and that code is shared on both the platforms. You write your code in C# and as a .Net developer it makes it easy to start things up, then there is some platform-specific code like showing picker with data-dependent upon the platform user is using the app on, which is much better than writing a different app altogether. The controls are the same for both platforms, the basic logic is the same but sometimes the developer must write some platform-specific code, and that is much easier.

The Language Being Used

So, the application is written in C#, the developer is either using Visual Studio 2017(because older versions are now not supported by Microsoft), for iOS you’ll need to pair a Mac host through the network, or you can develop your mobile app in Visual Studio for Mac.

Xamarin.Forms are not just using shared code, written in C# and build your app for Android and iOS (it also has UWP support, but we are not going to talk about it because now who uses a windows phone anyway). It is a lot more exciting when you come to know what it offers. As their official website says about Xamarin.Forms:

            “Xamarin.Forms is a cross-platform UI toolkit that allows developers to easily create native user interface layouts that can be shared across Android, iOS and Windows Phone.”.

As they say a cross-platform UI toolkit, it is not just a UI toolkit but everything you need to develop a cross-platform application is 100% native iOS and Android code for your app.

XAML

We use XAML, eXtensive Application Markup Language, to create layouts for the app. It was developed by Microsoft as an alternative for programming code for instantiating and initializing objects and organizing those objects in parent-child hierarchies. It is not required in Xamarin.Forms but it much more self-defining rather than writing equivalent code for designing and it is well suited for use in MVVM (Model-View-ViewModel) application architecture, it is used to design the view of app and that View is linked to the respective ViewModel code through XAML-based data bindings.

It is being adopted in several .Net technologies such as Silverlight, WPF (Windows Presentation Foundation), the Windows Runtime and the Universal Windows Platform (UWP).  And it is being used in Xamarin.Forms for designing layout for all the platforms the application is being developed for, and when it is cross-platform it is also a natively-based programming interface for iOS and Android.

XAML has some advantages over equivalent code:

  • Developers can create XAML files via a visual design tool and it is easily hand-written by developers.
  • XAML is concise and readable than the equivalent code.
  • The parent-child hierarchy in XAML is of greater visual clarity.

At first, you have to get a little familiar with it, but when you get to know things, it is much easier and faster to design layouts rather than using code.

Sample Code

We will now see how easy it is to use Xamarin.Forms with MVVM application architecture. We here have a view (XAML file), its code behind and a ViewModel.

First the View, this contains a hard-coded label, and a label that has binding text which is being set in the ViewModel:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App2"
             x:Class="App2.MainPage">
<StackLayout>
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!"
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
           <Label
            Text="{Binding HelloWorld}"
            HorizontalOptions="Center"
            VerticalOptions="CenterAndExpand" />
     </StackLayout>
      </ContentPage>

Now the code behind, here we will tell the View to get the ViewModel by binding it:

public MainPage()
  {
   InitializeComponent();
   BindingContext = new MainPageViewModel();

And now the ViewModel, from where we are setting the Label’s text on the View:

public class MainPageViewModel
     {
  private string _helloWorld;
  public string HelloWorld
  {
   get { return _helloWorld; }
   set { _helloWorld = value; }
  }
  public MainPageViewModel()
  {
   _helloWorld = "This is from ViewModel";
  }
}

The result will be this simple screen, showing a label with text set in View and a label that has its text set in ViewModel

“We Provide”

Here at Aciano, we provide our customers with the best quality software on whatever platform they want. Hence, we have Xamarin.Forms at the rescue as this is the framework that provides us with the possibility to develop apps for multiple platforms with a single code base which is working as a shared codebase, and voila! The app we get is native, means that we will be having the fastest and the best-looking apps. So, the customers are content at what they get as a product.

If we summarize the above stance we can elaborate that Xamarin.Forms are very powerful when it comes to developing cross-platform applications using .Net. It gives the developer a platform where not only the native code is shared across platforms but also the interfaces. It has a powerful data binding system that enables developers to get and set properties of controls via View-Model and provide a good user experience and change the view as per need. Xamarin.Forms provide us one technology to cater to all platforms, performance near to native, user experience just like a native app, developers do not have any compatibility with hardware-level issues, simplified maintenance and its own IDE (Visual Studio) plus Xamarin SDKs along with Xamarin test cloud. Therefore, create an application in Visual Studio with Xamarin’s SDK and test it in Xamarin test cloud no need to buy additional tools for any other thing, everything is built-in and available to the developer easily.