Xamarin Essentials

By Mark Reynolds
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Xamarin and Mono – a Pathway to the Unnatural

About this book

Xamarin is a Mono-based suite of products that allows developers to create iOS and Android apps using C# and .NET.

This learning guide is your first port of call to quickly learn Xamarin. Starting with the foundations by understanding the architecture behind Xamarin.iOS and Xamarin.Android, you will be guided through creating your first Xamarin.iOS and Xamarin.Android applications. In addition, you will learn how to utilize frameworks such as MvvmCross and Xamarin.Forms to increase the amount of code reuse. To conclude your journey, you will be shown how to develop your own Xamarin apps for distribution.

Xamarin Essentials is the guide to use in order to quickly gain practical working knowledge of developing robust commercial mobile apps using C# and sharing large portions of your code base across iOS and Android apps.

Publication date:
December 2014
Publisher
Packt
Pages
234
ISBN
9781783550838

 

Chapter 1. Xamarin and Mono – a Pathway to the Unnatural

This chapter provides an overview of the Mono project and the suite of Mono-based commercial products offered by Xamarin. To begin this pathway into the unknown, this chapter will cover the following topics:

  • Understanding Mono

  • Why you should use Xamarin

  • Installation of Xamarin.Android and Xamarin.iOS

  • Using Xamarin Studio and Visual Studio for development

  • Options for source control

 

Understanding Mono


Before we jump straight into a conversation about Xamarin, let's discuss a related topic: Mono. Mono is an open source cross-platform implementation of the .NET platform. This includes a Common Language Runtime (CLR) that is binary compatible with Microsoft .NET, a set of compilers for languages such as C#, and an implementation of the .NET runtime libraries. The Mono CLR has been ported to many platforms, which include the Linux and BSD-based operating systems (which are not limited to just Android, iOS, and OS X), Windows-based systems, and even some game consoles such as the Wii, Xbox 360, and PS4.

The Mono project was started by a Ximian, which was purchased by Novell. Xamarin now leads Mono.

 

The Xamarin product suite


Xamarin is a software company that offers a suite of commercial Mono-based products that allow developers to create apps for Android, iOS, and OS X using C# and the .NET framework. Xamarin's cofounder, Miguel de Icaza, has directed the Mono project since its inception in 2001.

Xamarin's primary product offerings are:

  • Xamarin.Android (formerly Mono for Android)

  • Xamarin.iOS (formerly MonoTouch)

  • Xamarin.Mac

Each of these products is licensed through an annual subscription with the following levels being available:

  • Starter: This subscription is actually free, but restricts the size of apps.

  • Indie: This subscription provides everything needed to build and deploy full-featured mobile apps, but can only be purchased by companies with five or less employees. This subscription also does not include the use of the Visual Studio add-in discussed in the Using the Visual Studio environment section.

  • Business: This subscription adds the use of the Visual Studio add-in as well as e-mail support.

  • Enterprise: This subscription adds access to a set of prime components, hotfixes, and enhanced support.

    Note

    A link for the pricing information can be found at the end of this section. For quick reference, please visit https://store.xamarin.com.

Xamarin also hosts a component store; a market place to buy and sell components that can be used in Xamarin apps. The components in this store can be distributed for free or sold, and Xamarin pays component vendors a percentage of the revenue collected from the sales.

Another service that Xamarin offers is the Test Cloud. As the name implies, this is a cloud-based testing service that allows teams to create automated testing capabilities for their apps that can be run against a plethora of physical devices. This is particularly important for Android developers as there are far more devices that need to be considered.

The following table provides useful links to additional information about the Xamarin suite:

 

Evaluating whether Xamarin is the right tool


Now that you have some background on Mono and the Xamarin suite of products, you might want to ask yourself: "Is Xamarin the right tool for my project?"

The benefits of using Xamarin are as follows:

  • It builds on your existing skills of using C# and .NET: Because of the huge number of features available to both the C# language and the .NET framework, it requires a huge investment of time and energy for developers to master them. Although you can argue that Java and Objective-C have similarities (being object-oriented languages), there is a real cost associated with transferring your proficiency in C# and .NET to make the same claim regarding Java or Objective-C. This is where Xamarin comes to your rescue; individuals and groups that have made a significant investment in C# and .NET might turn to it if they wish to develop iOS and Android apps due to the requirement of these skills.

  • Allows for reusability of code in cross-platform development: Although the Xamarin suite prevents you to create an app that can also be deployed to Android, iOS, and WP8, it compensates for this by providing you with the capability to recycle huge portions of your code base across all of these platforms. The general process that makes this all so much easier for you is that the user interface code and the code that deals with the device capabilities tend to be written for each platform. With this, things such as client-side logic (proxies and caching), client-side validation, data caching, and client-side data storage can potentially be reused, saving you a huge amount of time and energy. I have personally seen Android and iOS apps share as much as 50 percent of the code base and some report as high as 80 percent. The more you invest in the approach to reuse, the more likely you will achieve a higher percentage.

However, there are some drawbacks when it comes to using Xamarin:

  • Costs due to licensing requirements: The Xamarin suite or tools are all commercial tools and must be licensed, meaning there is a tangible cost of entry. You can check Xamarin's website for the current pricing.

  • Waiting for updates: You will find that there is some lag time between a new release of a platform (Android/iOS) and the corresponding release of the Xamarin products that support it. Xamarin has done a great job of releasing Xamarin.iOS on the same day when the new versions of the OS are made available. Xamarin.Android generally lags behind because Google does not make beta/preview versions available. In some ways, this delay is not a big issue at least for phone apps; the telecoms generally take some period of time before they provide the newest Android versions to be downloaded.

  • Performance and memory management: This is probably more of a concern for Xamarin.Android than Xamarin.iOS. As you will see in Chapter 2, Demystifying Xamarin.iOS, Xamarin.iOS essentially builds a binary executable much like those produced by using just Xcode and the iOS SDK. However, as we will see in Chapter 3, Demystifying Xamarin.Android, Xamarin.Android relies on deploying the Mono CLR and the communications between the Mono CLR and the Dalvik VM. In some cases, Xamarin.Android will allocate Java and C# objects to achieve some of the "magic" and "sorcery" behind developing in C# or .NET on an Android device. As a result of this, Xamarin.Android will affect both the memory footprint and execution performance.

  • Distribution size: There are a number of runtime libraries that must be distributed or linked with Xamarin apps. A discussion of the actual size and strategies to minimize the distribution size is reserved for the last chapter.

While the list of drawbacks might seem extensive, in most cases, the impact of each can be minimized. I have chosen to build out a Xamarin consulting practice because I place a high value on the benefits identified and feel like many groups that have a significant investment in C# and .NET will see the same value. If you are a group or an individual that places a great value on Xamarin's benefits, then you should certainly consider using it.

 

Learning C#


This book assumes that you have a working knowledge of C# and .NET. Since this might not be the case for some readers, we have included a few links to help you get up to speed. Xamarin provides the following link which presents C# from an Objective-C perspective: http://docs.xamarin.com/guides/ios/advanced_topics/xamarin_for_objc/primer

Microsoft provides a set of tutorials for learning C# available at: http://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx.

 

Installing Xamarin


Before moving on, we need to install Xamarin. This section will show you the steps to install Xamarin on both the Android and iOS platforms, notably Xamarin.Android and Xamarin.iOS, on both OS X and Windows.

Since Xamarin.iOS is dependent on the latest iOS SDK and the latest Xcode, both of these should be installed prior to starting the OS X install.

Tip

Both Xcode and the iOS SDK are free and you can download these installs from: https://developer.apple.com/devcenter/ios/index.action#downloads.

Also, note that you can install Xcode from the OS X App Store.

Likewise, Xamarin.Android is dependent on the latest Android SDK; however, the difference being that the Xamarin install will automatically download the Android SDK and install it as part of the overall install process. So, no separate steps need to be taken. If you already have installed the Android SDK, you have just been handed the opportunity to use it.

Installing Xamarin on OS X

To install Xamarin on OS X, go to www.Xamarin.com, download the OS X installer to launch it, and follow the directions. Be sure to choose to install both Xamarin.Android and Xamarin.iOS; Xamarin.Mac is optional.

The Xamarin.iOS Visual Studio plugin uses the build server called mtbserver to compile the iOS code on your Mac. If you plan to use the Visual Studio plugin, be sure to choose to allow network connections.

Installing Xamarin on Windows

Now, we move on to the Windows installation process. If you plan on using the Visual Studio add-in, Visual Studio will need to be installed prior to installing Xamarin.

To install Xamarin on Windows, you need to visit www.Xamarin.com, download the Windows installer, launch it, and then follow the directions. Be sure to install both Xamarin.Android and Xamarin.iOS.

 

Development environments


Developers have two options when it comes to IDEs: Xamarin Studio or Visual Studio. This section will show you how to develop apps for both Android and iOS through both of these studios.

Using the Xamarin Studio environment

Xamarin Studio is a customized version of the MonoDevelop IDE and this can be used to develop applications for Android, iOS, and OS X. Xamarin Studio is available on both OS X and Windows with highly advanced and useful features such as:

  • Code completion

  • Smart syntax highlighting

  • Code navigation

  • Code tooltips

  • Integrated debugging for mobile apps running in emulators or on devices

  • Source control integration with Git and Subversion built-in

If you look at the following screenshot, you will see how Xamarin Studio is shown with the Android user interface designer opened:

Using Xamarin Studio to develop Android apps

Xamarin Studio and the Xamarin.Android add-in allow the complete development and debugging of Android apps without use of any other IDEs. The Android UI designer can also be used from within Xamarin Studio.

Using Xamarin Studio to develop iOS apps

Xamarin Studio and the Xamarin.iOS add-in allow the development and testing of iOS apps when installed on a Mac with Xcode and the iOS SDK. All code can be written, compiled, and debugged from within Xamarin Studio. In general, the user interface XIB and/or storyboard files must be built within Xcode; Xamarin Studio provides a link to Xcode such that when a xib or storyboard file is double-clicked on, Xcode will be launched.

There is a caveat to this; Xamarin has an iOS UI designer built for Xamarin Studio, yet it has remained in an alpha status for almost a year. I have seen a number of posts on various forums indicating it is stable and safe to use, but Xamarin has been slow to clarify why it is still in alpha status and when it will move to a stable status. We will discuss the use of the iOS UI designer in more detail in Chapter 4, Developing Your First iOS App with Xamarin.iOS.

Using the Visual Studio environment

Xamarin for Visual Studio is an add-in that supports the development of the Xamarin.Android and Xamarin.iOS apps and is available to business and enterprise subscribers. This add-in can be used with any non-Express edition of Visual Studio 2010 through to Version 2013. Android apps can be completely developed using Visual Studio. In order to develop iOS apps, you will still need a Mac with the iOS SDK and Xcode to compile and create the user interface xib and/or storyboard files.

Tip

If you already have a license for Visual Studio and are comfortable with the environment, this add-in will be better suited to you than Xamarin Studio due to it being simple to use.

The following screenshot shows Visual Studio 2012 with the Android user interface designer opened:

The Android user interface designer

Using Visual Studio to develop Android apps

The Visual Studio add-in for Xamarin.Android allows the full development and debugging of Android apps without the use of any other IDE. This add-in provides the usage of the Android UI designer from within Visual Studio. For those that have the appropriate licenses and are comfortable with Visual Studio, this might be the best option for Android development.

Using Visual Studio to develop iOS apps

The Visual Studio add-in for Xamarin.iOS allows you to develop and test iOS apps, but only in conjunction with the use of a Mac with both Xcode and the iOS SDK installed. The iOS code must be compiled and executed on a Mac using the Xamarin's mtbserver. Xcode on a Mac must also be used to develop the user interface xib and/or storyboard files for an iOS app. We will discuss this configuration in more detail in Chapter 4, Developing Your First iOS App with Xamarin.iOS.

Note

Solution and project files created and used by Xamarin Studio are completely compatible with Visual Studio. This gives teams the flexibility to choose which IDE to use and they can easily change throughout the duration of a project.

Comparing IDEs

The advantages and disadvantages of adopting each IDE are shown in the following table:

IDE

Pros

Cons

Xamarin Studio

Available for all Xamarin subscription levels

Runs on Windows and OS X

Limited number of productivity add-ins available

Does not offer support for the use of TFS for source control

Visual Studio

Most C# developers are already familiar and comfortable with Visual Studio

Allows the use of popular productivity add-ins such as ReShaper and CodeRush

Allows the use of TFS for source control and issue tracking

Requires a business or enterprise subscription of Xamarin

Requires a license of VS

Runs on Windows only

For iOS development, requires a more complex configuration in which VS must communicate with a Mac running Xcode to perform builds and UI development must be done with Xcode

Version control

Version control can be a challenge anytime you have a diverse set of development tools, and Xamarin certainly adds diversity to most shops. The challenge is making it easy to share and manage code from all of the different IDEs and client apps that folks will be working with; many times they do not have access to the same repositories. Since the benefits of using Xamarin are very attractive to existing .NET shops, many Xamarin developers will find themselves working in environments already committed to using Microsoft Team Foundation Server (TFS). Unfortunately, it's not always easy to connect to TFS from non-Microsoft tools. In the case of Xamarin Studio, there is an open source add-in that cannot be directly supported by Xamarin and can be challenging to configure.

Other version control systems to consider include Git and Subversion. Xamarin Studio contains built-in support for both Git and Subversion, and add-ins for both of these tools exist for Visual Studio. The following table contains useful URLs to download and read about the various add-ins:

Add-in

URL to access it

TFS add-in for Xamarin Studio

https://github.com/Indomitable/monodevelop-tfs-addin

Git for Visual Studio

(VS2013 has built-in support)

http://msdn.microsoft.com/en-us/library/hh850437.aspx

(VS2012 requires a free plugin)

http://visualstudiogallery.msdn.microsoft.com/abafc7d6-dcaa-40f4-8a5e-d6724bdb980c

Subversion add-in for Visual Studio (by VisualSVN)

http://www.visualsvn.com/visualsvn/?gclid=CMmSnY-opL0CFa07MgodDksA5g

Like many aspects of software development, there is not a "one size fits all". The following table outlines some of the pros and cons to consider when deciding on a source control solution for Xamarin projects:

VCS Tool

Pros

Cons

TFS

Already in use by many shops that will consider Xamarin.

Free add-in for Xamarin Studio.

Xamarin Studio add-in has been known to be problematic to use in the past.

Git

Built-in support in Xamarin.

Free add-in available for Visual Studio 2012 and 2013.

Difficult to share and synchronize code with other teams in a large organization that might be using TFS for their C# code.

Subversion

Built-in support in Xamarin Studio.

Commercial add-in for Visual Studio.

Difficult to share and synchronize code with other teams in a large organization that might be using TFS for their C# code.

If you already have a significant investment in using TFS, try to make that work for your Xamarin development as well. This can be done by either having developers use Visual Studio or trying your luck with the TFS add-in for Xamarin Studio.

 

Summary


In this chapter, we provided an introduction to Mono and the suite of commercial products offered by Xamarin and considered the pros and cons of using Xamarin. We also went through the installation process and took a first look at the IDE options available to developers.

In the next chapter, we will take a look at the architecture of the Xamarin.iOS product.

About the Author

  • Mark Reynolds

    Mark Reynolds is a software enthusiast who has worked in the industry for nearly 30 years. He began his career with Electronic Data Systems, building and supporting systems for the manufacturing sector. Over the years, Mark has worked with start-ups and Fortune 100 companies across a diverse set of industries. In 1993, he started a consulting practice that focused on delivering training and mentoring services in the areas of software architecture, design, and implementation. With the rise of mobile computing, he has focused his practice on designing, developing, and delivering mobile software solutions.

    Mark recently published his first book, Xamarin Mobile Application Development for Android, Packt Publishing. His private consulting practice is based in Allen, TX, USA, where he resides with his wife and son.

    Browse publications by this author
Book Title
Access this book, plus 7,500 other titles for FREE
Start FREE trial