Introduction to building cross platform native apps using Xamarin

-

Xamarin is a platform for developing fully native applications for iOS, Android and Windows, with a shared code base. Xamarin apps are written in C# and can make use of most .NET libraries.

Why Xamarin?

  • Xamarin apps are native so the performance is great.
  • Everything that can be done with native app development can be done with Xamarin. All native Android, iOS and Windows API’s are available.
  • The UI is native. Xamarin Android apps use the same design guide lines as other Android apps. This is important because the user will have a similar user experience with your app as with other apps. Same goes for iOS and Windows apps.
  • You only need to know or learn one language to develop for all three platforms.

 

Some background information

Some of you might be confused now because .NET only runs on Windows right? No that’s not completely true so I want to start with some background information about CLI and Mono.

In 2000 Microsoft released the Common Language Infrastructure (CLI). This is an open specification and is standardized by ISO and ECMA. The CLI describes how high-level language applications can run on different platforms without changing the code. There are a few implementations of the CLI like .NET Framework, .NET Core, Mono and more. The .NET Framework is the most used and most well-known implementation which only runs on Windows.

Mono is a CLI implementation that runs on Windows, Linux, Mac and even Embedded systems. Mono makes it possible to write applications with WinForms, WCF, ADO.NET, Entity Framework and more in languages like C#, F#, VB.NET, Python and more. Mono is also available for iOS as Xamarin.iOS, previously named MonoTouch and Xamarin Android, previously named Mono for Android and MonoDroid.

How does Xamarin work?

When you scaffold a Xamarin app you get multiple projects. The number of projects you get depends on which platforms you want to support. The first project is a class library which contains all the code shared between all the supported platforms. You also have one project for each platform you want to support. Those projects contain all of the platform specific code.

Xamarin contains binding for the iOS and Android SDKs. So Xamarin code looks pretty much like the code you would write when developing a native app. I’ve written a very simple Android example to show you what it looks like. It’s an app which fetches some information about countries from a webservice and show them in a list. It also has a switch. If the switch is turned on it shows the country codes and when turned off the app won’t show the country codes. The code should look very familiar to Android developers.

namespace XamarinCountries
{
    [Activity(Label = "XamarinCountries", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        private IEnumerable _countries;
        private Switch _countrySwitch;
        private ListView _countryList;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            _countryList = FindViewById(Resource.Id.listCountries);
            _countrySwitch = FindViewById(Resource.Id.swCountryCode);
            FetchCountries();
            RedrawList();
            _countrySwitch.Click += (sender, args) =>
            {
                RedrawList();
            };
        }
 
        protected void RedrawList()
        {
            string[] items = new string[] { "No countries available" };
            if (_countries != null && _countries.Count() > 0)
            {
                items = _countries.Select(c => c.Name + (_countrySwitch.Checked ? " - " + c.CountryCode : "")).ToArray();
            }
            _countryList.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, items);
        }
 
        protected void FetchCountries()
        {
            CountryRepository repo = new CountryRepository();
            this._countries = repo.GetCountries();
        }
    }
}



    
    

But how about Windows? There is actually no such thing as Xamarin.Windows. Windows apps are developed the same way as you would normally develop native Windows apps. The only difference is that the app will use the shared code library which is also used by the Android and iOS apps.

What do you need to get started with Xamarin?

To develop with Xamarin you can use Windows or OS X. There are two IDEs available for Xamarin. The first one is Visual Studio which is only available for Windows and the second one is Xamarin Studio which is available for Mac and Windows.

For developing Android and iOS apps you can use both Mac and Windows. But if you want to use Windows you still need a Mac for the iOS app because Xamarin relies on the iOS build tools which are only available for Mac. You’ll have to use the Mac as a build agent. You can connect the app by using a wizard in Visual Studio (see picture).

Xamarin Mac agent

The Windows pc will send the code to the Mac and the Mac will create the iOS packages and will run the app on a simulator on the Mac, or an iOS device connected to the Mac.

Developing Windows apps is only possible on Windows. If you still want to work on a Mac you can develop Android, iOS and shared code on the Mac and use source control to continue working on your Windows app on Windows.

Click here for more info on this.

Later, I will write a blogpost about Xamarin forms, a toolkit to create a shared UI for all three platforms and more information about the Xamarin platform.

Sources: