devxlogo

Cross-platform Development in C# with Xamarin

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist