May 29, 2020 by Lucas Stirpe
First Steps in Xamarin
One Software Developer’s Experience Using Xamarin
Xamarin.Forms is an open source, cross-platform framework from Microsoft for building iOS and Android apps with .NET from a single shared codebase.
As a .NET Full Stack Software Developer, I recently took my first steps into Xamarin by cloning two apps and diving into the code.
One of the apps, called “FoodAppSample,” seemed to be an ideal app to start learning about Xamarin.
To follow along, click here to find the url of the repository.
Next, your app will be introduced into this Emulator and you´ll be able to do any test cases, complementing all the possible functions of the phone, such as making your app interact with another one, trying the activation of the notifications, and so on.
It should also be noted that to compile an app in iOS, you need to have a MacBook with Xcode.
The typical structure of a Xamarin Forms App
As you will see, the structure used is a very simple MVVM.
If you´ve never done anything in Xamarin, you might be asking what is the purpose of those three projects.
The first project is the general one. It contains the whole app without discriminating whether it will be run on Android or iOS. This is where we will find the “single shared codebase.”
The second project contains specific code for the app when it is run on Android. The same goes for the third project, but for iOS.
Why is this app using MVVM?
MVVM is the perfect separation of responsibilities for an app built in Xamarin.forms. In fact, this pattern was invented with Xamarin in mind.
MVVM consists of three layers: the Models, the View, and another layer in the middle of the last two, called View-Model.
1.The Model: The Model contains the data of the domain (Entities with their own attributes) and the business logic.
2.The View: The View represents the User Interface with its functionality.
The View is responsible for defining the structure that will be shown on the screen. This can be defined with XAML or in a Code-Behind, but it is recommended to use XAML.
Why is XAML recommended? The User Interface of a Xamarin app handles a lot of events and, therefore, a lot of code is involved.
Data binding automates this job and renders the event handlers unnecessary. Data bindings can be implemented either in code or in XAML, but they are much more common in XAML where they help to reduce the size of the code.
3.The View-Model: The View-Model is responsible for communicating the models and the views.
For example, if we have an entity called “book” and we wish to display a list of books, the method “DisplayListOfBooks()” should be present in this layer.
If you´ve never used the structure MVVM, it’s not complex to learn. It’s also not difficult to learn to use the XAML language.
The most complex part of learning Xamarin, at least in my short experience, is to use both projects focused on the different platforms (iOS and Android).
Using the Emulator at the beginning can also become a little tedious, but it doesn’t take long to get used to the tool.
So, is Xamarin.forms a good choice?
You must consider that Applications compiled in Xamarin are larger than a native application. However, If what you want is to develop a mobile application that works for all platforms and that does not involve too much cost or effort, Xamarin is the choice for you.