Developing Mobile Applications in .NET with Xamarin

Mobile application development is now quite popular these days; there are plenty of frameworks available. Xamarin is a very popular framework used to develop mobile applications. In this article, we will explore the Xamarin framework for developing Android applications in .NET. This is the first part of a two-part series of articles on Xamarin. In this first part of the series of articles on Xamarin, we will learn what Xamarin is all about and how we can set up our system to be ready for developing Android applications using Xamarin for Visual Studio.

The first thing you need to do is download and install Xamarin for Visual Studio so that you can get started developing iOS and Android applications using Xamarin and C# in the Visual Studio IDE.

Getting Started

The minimum requirements for getting started with Xamarin for Visual Studio are given below:

Windows 7 or higher

Visual Studio 2010 Professional or higher

Xamarin's plug-in for Visual Studio

You would also need to install Xamarin from the Xamarin website after signing up for a new account. Here's the link: http://www.xamarin.com/.

It should be noted that Visual Studio Express Edition will not work because it won't support the plug-ins. Also, note that you should have a Xamarin account if you want to develop applications using Visual Studio; you would otherwise see a popup prompting you to create one and then use Xamarin in your Visual Studio IDE. Once all is set up, you should check the Android settings in your Visual Studio IDE by clicking Tools -> Options -> Xamarin -> Android Settings.

Your Android setting should point to the path where Android SDK is located in your system.

Working with Xamarin

Note that all Android applications basically start with an Activity; this typically would have an AXML layout. Note that the AXML format is actually an XML format used to describe the user interface on Android. Here is what the Main.axml file looks like.

You can use the Visual Studio IDE to develop your Android application and deploy it to an emulator that is configured to match your target Android machine.

To start developing an android application using Visual Studio IDE, follow these steps:

Click File -> New Project.

Select the template you need to use.

You will see many templates there; select Android Application.

Provide a file name for the project and click OK.

A new Android project will be created in your Visual Studio IDE. When you create a new Android project using Xamarin, you basically would find the following solution folders:

Assets: This would contain the arbitrary files, such as text, xml, fonts, music, and video the application would need.

Properties: This would contain the assembly metadata.

Resources: This would contain the application resources; in other words, strings, images, and so forth. It can also contain the declarative definitions for the user interface of your Android application.

You can place any assets that you would want to be deployed, along with your application, in the Assets folder. You then would be able to access these assets by using the AssetManager, as shown in the following code snippet:

An activity should derive from the Activity class and you can mention the label, the main launcher, and icon using the Activity attribute on top of your activity class. Here's how an Activity class would look.