Using C# for Cross-Platform Development With Xamarin
DZone sits down with Jim Bennett to discuss the Xamarin platform and it's benefits for cross platform native development.
Join the DZone community and get the full member experience.Join For Free
The battle of the mobile app development platform rages on as strong as ever. Whilst native apps are generally regarded as providing a better experience in general, they are inherently more expensive as they require 2 separate development teams, often working at different paces.
Meanwhile, mobile application frameworks have been maturing and evolving. Platforms like PhoneGap and Selenium allow programmers to develop applications in one language and compile it down to iOS and Android. Whilst being very convenient they are generally regarded as slower and providing a less than native experience for the end user.
This was why I was so surprised to hear about a new platform that allows developers to write in C# and compile down to all of the major mobile platforms called Xamarin. It claims to give a native experience on all platforms and has been quickly building a reputation. I sat down with Jim Bennett, a Xamarin certified developer to ask him some questions about Xamarin and mobile development.
Hi Jim, thanks for taking the time. Can you start by giving us a brief background on Xamarin? How long has it been around for, who’s behind the scenes?
Thanks for having me. Xamarin all started from Mono - work done by Miguel de Icaza to port Microsofts .Net framework to Linux. Over the years Mono grew and got ported to iOS and Android (MonoTouch an MonoDroid). A company was formed to create the tooling that sits on top of it, and that is Xamarin as we know it now.
Basically the code is still Mono, still an open source port of .Net and on top is the Xamarin tooling and ecosystem. This converts it into a very powerful developer experience, with a fully featured IDE, development and testing tools, wrappers around a huge range of native libraries and all the developer support you need.
It’s a successful company, well funded by venture capital and supported by large tech players.
It has very close ties to Microsoft. Over the past couple of years Microsoft has changed its strategy to focus on open source, and they are open sourcing the core parts of .Net, along with their compiler. The open source Mono core is still there, but parts of it are being replaced with Microsofts own open source implementation. I can see in a few years time Mono being retired and completely replaced by the opens source work from Microsoft.
It sounds crazy that I can write in C# which will then compile to Android, a Java platform, and iOS in Objective C. How does it work? Is it really capable of achieving a native experience?
Yes - it is really capable of creating a native experience. Despite coding in C# and using the same libraries and frameworks a C# developer uses on Windows you do actually get native code.
This is achieved by two things.
Firstly the compiler. This produces native code on iOS which runs inside the same runtime as any other iOS app. On Android it either produces byte code that runs inside a .Net VM on the device where it gets just-in-time compiled to native java byte-code, or with the more recent updates it can compile to native byte-code ahead of time. So it’s not an emulation layer, it’s compiled to native code.
Secondly - the libraries. Unlike other frameworks which abstracts away the native components, Xamarin actually provides wrappers around the platform specific APIs. This is both the big upside and the big downside to Xamarin.
The downside is unlike other web-based frameworks you have to write some device specific code. Your UI layer and your interaction with the device is all written in C# but calling C# wrappers around the native APIs. For example in iOS you have a UIViewController class in Objective-C which is the controller for a view. In Xamarin there is also a class called UIViewController which you use and behind the scenes it refers to the same thing. On Android there is an Activity class that wraps the native Java activity. This means that you can’t share UI code or device specific code between projects.
The upside is you get a totally native experience with everything supported. Pretty much as soon as an SDK is available from Apple, Xamarin provide the wrapper for it so you can use it.
Saying this, there is a push from Xamarin to try to create a totally cross platform experience, and they call this Xamarin.Forms. The ides behind this is to wrap the platform specific code in generic wrappers that you use, but still allow you to use native code where necessary. This allow you to build an app with buttons, lists etc and have it build and run on iOS, Android, Windows Phone 8.1 and (with beta support) Windows 10 across all devices. The magic here is that it is not a web view styled to look like the device, it is native. When you add a button to you Xamarin.Forms view it is an actual button - a Button on Android, a UIButton on iOS etc. The
This is a big push from Xamarin, especially for LOB apps and prototypes, and the architecture allows you to wrap native code in a way that can be called from all platforms. For example if you wanted to build a tinder-style swipe control you could build it for iOS, Android and Windows, then create the wrapper and use it from an Xamarin.Forms projects. Even large tool vendors like DevExpress and Telerik have got behind this and are creating controls that can be used in Xamarin.Forms. There is also a big push towards plug-ins which wrap the platform specific hardware and software features (e.g. GPS, accelerometers, cameras) in a lower-common-denomnator way so you can access them from cross platform code.
Xamarin is not just limited to mobile - as well as the support for Windows 10 universal apps in Xamarin.Forms they also have Xamarin.Mac for creating mac apps. They even have game support so you can write a game using CocosSharp (a port of Cocos2D, a popular game engine) and run that on all platforms.
Often the success of these frameworks is in the community. Is Xamarin still very niche or has it got a community behind it? Are there any well known applications in the store that are developed using Xamarin?
The community is pretty strong, with a lot of events organised by user groups, a strong team of developer evangelists spreading the word, and great support from the company. They have on-line forums where you can get pretty much anything answered including getting feedback from the developers themselves. They have a conference that runs every 18 months bringing together developers from all around the world. Microsoft are big supporters - even down to having an installer for the free starter edition bundled with the installer for Visual Studio 2015 and working closely with Xamarin on their efforts to open source the Microsoft .Net framework. They have strong links to Oracle and IBM for enterprise development.
It's been used by some big name players too, like Tesco and Uber.
How do you feel Xamarin sets itself apart from the other frameworks like PhoneGap and Selenium?
It’s set apart by being a native experience - you are not running a web browser hosting a web app, you are running native code. So the UI looks native - if the UI changes your app changes to match. For example with iOS every now and again they overhaul the UI and you can see which apps are PhoneGap apps because they still look like the old UI, native apps immediately look like the new UI. This native experience also means you get access to everything the phone has to offer without waiting for PhoneGap support. As soon as Apple added 3D touch you could use it from a Xamarin app, not so easy with PhoneGap.
You also get native performance, running on the hardware not inside a web browser.
There is a dedicated course/qualification for Xamarin. Can you tell me about that, and what benefits developers get from taking it?
Xamarin offer a university - this is a combination of pre-recorded lectures and talks, along with a large array of live, instructor led courses. These cover everything you need to get started, from courses in C#, to courses covering all the aspects of Android or iOS development. The instructor led courses are really well done, with good quality lecturers, well prepared course materials and small classes so you can interact with the instructor and get any questions answered. They also offer ‘office hours’, so one-on-one time with an instructor to discuss anything you want.
If you complete a specific range of courses you can do the exam to become a Xamarin Certified Developer. In theory this can improve your employability but I’ve not see this being of real benefit.
The university is either really cheap or really expensive - depending on your point of view. It’s a couple of thousand US dollars but for what you get I think it’s well worth it.
Does Xamarin handle Android fragmentation?
It handles it as well as Android does! So it supports everything you need with all the compatibility libraries that Android supports, but that’s it from the coding side.
They do have one ace up their sleeve though - Xamarin Test Cloud. This is an online array of physical devices you can run your app on. Using their UI test framework you can write UI tests which you start by running locally on simulators or actual devices, then you can push these to their cloud and run your tests across a huge range of devices and OS variants. Their tests allow you to see if there are any crashes, grab screenshots of each stage and see where things failed - the UI is showing wrong data or is on the wrong screen for example. This is a great way of seeing if there is a particular OS variant or device that causes you problems.
They also have a tool to track usage called Insights which provides crash reporting as well as user data - so you can use Insights to see which devices your users have and run your tests on those devices. it helps you be smarter when working out which of the 14,000 android variants you want to test on. For example, if there is a particular device that causes your app to crash you can see how many people it affects and make a call on how much effort there is to fix it. And from the usage data you get from Insights you can work out how to re-create the crash and build test cases that you can run against those devices in the Xamarin Test Cloud without ever having to buy one of those devices.
Where should we go to if we want to learn more and get started developing?
Xamarin.com is the place to start, you can download a trial and see everything they have to offer. If you just want to talk Xamarin then search meetup.com for your nearest Xamarin meetup.
Follow @SambaHK on Twitter for more like this.
Opinions expressed by DZone contributors are their own.