Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Cross-platform Development in C# with Xamarin

Learn now Xamarin provides a well-executed platform for developing mobile applications using C# and the .NET framework.


advertisement

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.



   
Gigi Sayfan is the chief platform architect of VRVIU, a start-up developing cutting-edge hardware + software technology in the virtual reality space. Gigi has been developing software professionally for 21 years in domains as diverse as instant messaging, morphing, chip fabrication process control, embedded multi-media application for game consoles, brain-inspired machine learning, custom browser development, web services for 3D distributed game platform, IoT/sensors and most recently virtual reality. He has written production code every day in many programming languages such as C, C++, C#, Python, Java, Delphi, Javascript and even Cobol and PowerBuilder for operating systems such as Windows (3.11 through 7), Linux, Mac OSX, Lynx (embedded) and Sony Playstation. His technical expertise includes databases, low-level networking, distributed systems, unorthodox user interfaces and general software development life cycle.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date