Getting started with Xamarin

Tips for the cross-platform developing framework

Andrei

Andrei

Software Engineer at Softvision
Andrei Lazarescu is a programmer with six years of commercial developing experience who is able to quickly pick up and use new technologies, tools and software applications. Andrei is a dedicated team member with a passion for video games and the gaming industry. He is patient and good problem-solver who is known for creative skills, commitment to efficiency and delivering work ahead of schedule
Andrei

Latest posts by Andrei

What is Xamarin?

Xamarin is a cross-platform developing framework enabling C# developers to develop native applications for Windows Phone, iOS, and Android. It is important to realize straight from the start that Xamarin will not spare you the burden of learning platform specific workflows.

Installation

The developing environment will be configured based on the targeted platforms, and you can choose from three setups: Windows, Mac, and Windows and Mac.

Note: There are ways to get around this, please check Xamarin Live Player(beta) and Gorilla Player

As illustrated above, we can see that for maximum platform coverage we will need to choose

Windows with Mac configuration, pairing the two computers together and then using the Mac as a build machine for iOS projects. This will not be possible if the version of Visual Studio is not compatible with the version of the Mac OS. For example Visual Studio 2017 needs Mac OS High Sierra to work properly, otherwise, an error message will appear.  In case of Android and Windows projects, the compilation will take place locally.

Compilation

At compilation time the C# code will be transformed into native code, resulting in a compilation for each platform, unlike Windows Universal Applications where you compile once and run on multiple Windows 10 devices. So, if we plan to develop for all three platforms, we will have three projects containing platform-specific code.

You must be wondering by now, why use Xamarin and why not use an application container of some sort? Well, by using application containers developers cannot take full advantage of all the features that the operating system has to offer and here is where Xamarin comes into play helping developers to share non-platform specific code to all platforms, saving time and money.

The code can be organized depending on its purpose into Shared Projects, Portable Class Library, and .NET Standard Library. As we can see below each solution can have its advantages and disadvantages.

Shared Projects

Pros:

  • Compiler directives.

Cons:

  • No output DLL.

Portable Class Library

Pros:

  • Output DLL.

Cons:

  • No compiler directive.
  • Small subset of the .NET Framework

.NET Standard Library

Pros:

  • Output DLL.
  • Larger subset of the .NET Framework

Cons:

  • No compiler directive.

Architecture

From an architectural point of view, the code sharing will not work if you do not use layers while building the application. In the below figure we can see a representation of such an architecture.

Note: Shared code must not contain reflection, iOS does not support it.

  • Data Layer – contains specific code for working with the data storage.
  • Data Access Layer – wrapper around the Data Layer that is used to retrieve data from the data storage without exposing implementation details to the caller
  • Service Access Layer – used to retrieve data from the data services.
  • Business Layer – contains business logic specific for that type of application. (Layer can contain also platform specific code, separated by compiler directives if organized in a Shared Project)
  • Application Layer – contains platform specific code defining UI components behaviors and more
  • UI Layer – contains platform specific code defining UI components

To facilitate building those layers, design patterns can be used. Below are some examples of such patterns:

  • MVVM – Model View ViewModel (Details here)
  • MVC – Model View Controller (Details here)
  • Façade (Details here)

Frequently met challenges

In this section, we will talk about the most common challenges encountered in the development of traditional applications and how we can deal with them in cross-platform development.

File Access – is one of the frequently done operations in applications. Every time we open files, read from them and write to them. For Android and iOS we achieve this by using Direct Access, on Windows 8 and Windows 10 it is Windows Storage and on Windows Phone, it is the Isolated Storage.

Doing this on all three platforms can be challenging, one must write code to detect the platform and then handle these operations on each platform. Fortunately, there is PCL Storage that does just that.

Database – most applications use databases to store different kinds of data. One lightweight solution is SQL Lite, which does not require a SQL Server and can handle most of the mobile application needs. There is an ORM for SQL Lite, it is called SQL Lite Net. ADO.NET can also be used.

Remote Operation – could be performed by using one of the Web Access .NET APIs, such as: HttpClient, WebClient or HttpWebRequest. Also, third-party components like RestSharp and ServiceStack could be used.

Threading – the Task Parallel Library works but fetching the UI thread is done differently on some operating systems, therefore an adapter must be implemented in order to overcome those differences.

A few insights about Xamarin.Forms

We talked a lot about code sharing in the previous sections but nothing about the user interface, it was treated as platform-specific code. Here is where Xamarin.Forms come into play. It is a cross-platform UI framework designed to further facilitate code sharing.

 Note: This will not help you avoid platform specific workflows like Storyboards or Activities

As we can see in the above illustration, components can be defined once in a Shared Project and styled for each platform in particular. This is useful when building custom components, defining them once and implementing the way they look, further limiting code duplication.

Unit Testing

Doing unit testing in this cross-platform environment can get a bit complicated, using just one unit testing framework will not work. Lower layers can be tested using the preferred .NET Unit Testing framework but for the platform-specific code we need to use one that is compatible with the operating system:

  • iOS: Touch.Unit
  • Android: Andr.Unit
  • Windows Phone: Windows Phone SDK
  • Windows Universal Platform: TestTools.UnitTesting and System.Test

More information for further training

 

Share This Article


Andrei

Andrei

Software Engineer at Softvision
Andrei Lazarescu is a programmer with six years of commercial developing experience who is able to quickly pick up and use new technologies, tools and software applications. Andrei is a dedicated team member with a passion for video games and the gaming industry. He is patient and good problem-solver who is known for creative skills, commitment to efficiency and delivering work ahead of schedule
Andrei

Latest posts by Andrei

1Comment
  • Syed Ameer
    Posted at 06:38h, 30 May Reply

    Very good article to start with.

Post A Comment