Cross-platform Development in C# with Xamarin

C# is a fantastic programming language and .NET is a fantastic runtime. It was developed by Microsoft as an answer to Sun’s Java at the dawn of the previous millennium and gained a lot of success and traction in certain domains like enterprise software, line of business application and games. Mono?is a 14-years old monumental open source project that provides a cross-platform implementation of the .NET framework. With Mono you can run .NET applications on Linux and MAC OSX. It’s hard to perceive what kind of effort and determination is required for such a project. Xamarin?is a for-profit company started by Mono’s founder, Miguel de Icaza, that sponsors the Mono project (along with hundreds of open source developers) and provides frameworks, tools for development environment for building mobile applications using C# and the .NET framework on iOS, Android and Windows phone (of course).

Xamarin’s Platform

While Mono is open source and free Xamarin’s platform is NOT. They have several pricing plans and a limited free tier. If you plan to do any serious development prepare to pay.

Xamarin is about letting you develop native applications for multiple targets (iOS, Android, Windows phone) using as much shared code as possible, but without sacrificing the native look and feel or access to specific device capabilities.

This means that all your business logic, networking code and data management can be developed in shared platform-agnostic projects and the platform specific-code for each platform is separate. There are a couple of options to package platform-agnostic code, depending on whether or not you plan to use the shared code only in your application or reuse it across multiple applications.

For the platform specific code Xamarin provides full access to each target platform using C# and also the generic Xamarin.Forms that allow for writing a single codebase that will still use native widgets.

The Demo Application

The demo application fetches the #1 featured article on HackerNews and displays its title and URL. It has two separate projects for iOS and Android and a shared project used by both. The code is available here.

Here is what it looks like initially:

When you click the button the text changes to “Fetching…”:

Once the article title and url are fetched they are displayed:

The Structure of a Xamarin Project

A typical Xamarin project has three parts–a shared project, an android project and an iOS project. In the shared project you put all the platform-agnostic code as well as code that can use platform-specific code exposed through a cross-platform interface or library. The OS specific projects are for the rest, which is typically the UI layout (although UI state management can often be done in the share project).

Here is the structure of the demo application. Note that the familiar .NET solution and projects are used.

The Shared Project

In the shared project called “XamarinPlayground” there is a single class called FeatureFetcher. It is responsible for asynchronously fetching the #1 article on HackerNews. This is pure C#/.NET code that uses the awesome async programming support of the .NET framework and the native sytax of C#. It can be used as is in any .NET program on any OS. It works by sending an async HTTP GET request to “https://news.ycombinator.com/news” and once the response is received it parses the returned page and extracts the title and URL of the first article using crude text manipulation and the nstore the result in the Title and URL attributes.

Here is the code:

The Android Project

The Android project called “XmarinPlayground.Droid” contains many directories and files that were all generated by Xamarin. They correspond to standard Android resources, files and artifacts required when using the Java environment such as manifest, layout and activity.

Let’s focus on the MainActivity.cs file. Here is the code:

First of all, it is a C# class! How cool is that that you can develop for Android using C#? Note the Activity attribute and the Activity subclass. Those two little decorations make this class a full-fledged Android activity. In the OnCreate() method, which is the only method, the content is set using the main layout and the button’s click handler is bound to an async delegate that on-click changes the buttons text to “Fetching…” and calls the FeatureFetcher.Fetch() asynchronously. Finally, when it returns it displays the fetched URL and title and resets the button’s text. Nothing is ever blocking and the UI remains responsive the whole time.

The iOS Project

The iOS project called “XmarinPlayground.iOS” similarly contains many directories and files that were all generated by Xamarin and are needed for iOS development.

The file that corresponds to the Android MainActivity.cs file is the ViewController.cs. Here is the code:

The logic is identical to Android’s MainActivity.cs, but the boilerplate is different (e.g. ViewDidLoad() method instead of OnCreate()) and the interaction with UI components is different.

Xamarin provides a cross-platform Forms library that abstracts away some of these differences.

Conclusion

Xamarin provides a well-executed platform for developing mobile applications using C# and the .NET framework. You can share a lot of code, but maintain full control on the look and feel on each target platform and use native capabilities and APIs.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

Windows Logging is one of the vital aspects of any Windows system administration. However, it is mostly overlooked until the system develops a problem. This is because logs contain important information needed to troubleshoot and resolve system issues.

The Fundamentals of Windows Logging

Windows Logging is one of the vital aspects of any Windows system administration. However, it is mostly overlooked until the system develops a problem. This is because logs contain important information needed to troubleshoot and resolve system issues. Without it, tech experts might find it difficult to track a computer’s

Interstitial Ads: Best Practices for Successful Campaigns

Interstitial Ads: Best Practices for Successful Campaigns

Interstitial Ads: Best Practices for Successful Campaigns Interstitial ads are full-screen advertisements that appear to grasp the attention of on-site prospects, creating opportunities for brands seeking effective ways to communicate their proposition of value. With such an attention-grabbing format and high-impact visuals, it’s no wonder why interstitial advertising is proving

positive contribution tech

Technology’s Positive Contributions to Society

Technology has and continues to improve our lives. From the business world to the world of medicine, and our day-to-day lives, you can’t go a day without interacting with at least one form of technology. While some fear technology may be going too far, there are many ways in which

How to Choose From The Best Big Data Platforms in 2023

How to Choose From The Best Big Data Platforms in 2023

As big data continues to become increasingly popular in the business world, companies are always looking for better ways to process and analyze complex data. The process critically depends on the platform that manages and analyzes the data. In this article, we will provide a guide to help you choose

Why transparent code is a good idea

Why Transparent Code is a Good Idea

Code is used to make up the apps and software we use every day. From our favorite social media platforms to our online banking services, code is the framework used to build these tools that help make our lives easier. Code is complex. Software today requires large teams of programmers