Book Image

Xamarin 4 By Example

By : Engin Polat, Mark Radacz
Book Image

Xamarin 4 By Example

By: Engin Polat, Mark Radacz

Overview of this book

The mobile app market is increasing exponentially every year. Xamarin Studio with its modern and powerful IDEs makes creating applications a lot easier by simplifying the development process. Xamarin will allow you and your team to create native applications by taking advantage of one of the most evolved programming language in the world: C#. This book will provide you with the basic skills you need to start developing mobile apps using C# and Xamarin. By working through the examples in each chapter, you will gain hands-on experience of creating a complete app that is fully functional by all means. Finally, you will learn to publish the app you created on the app market. Each project in this book will take you one step closer to becoming a professional app developer.
Table of Contents (16 chapters)
Xamarin 4 By Example
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
7
Monkey Puzzle Game – Processing Images

Take off


Before we learn what cross-platform is, we will explore the meaning of platform specific native apps.

A native app is an app that uses the native Software Development Kit (SDK), and compiles and runs on one specific platform.

It is usually developed using the default programming language of the SDK.

We can write a native iOS app using Xcode as Integrated Development Environment (IDE) and Objective-C/Swift as languages, taking advantage of the iOS SDK.

A native Android app is written using Eclipse or Android Studio as IDE, Java as language, and the Android SDK as development kit.

A native Windows Phone app is written using Visual Studio as IDE, C# as language, and .NET as Framework.

This is the classic way to develop apps and for some developers, it is still the best way.

Platform specific native development gives some advantages. The first is that we can rely on the OS manufacturer publishing stable updates and we can always be one of the first to use them.

Also, apps written with native tools and languages have performance, security and better user experience advantages. Hybrid apps basically mobile focused web apps built with HTML5 and JavaScript, wrapped in a native container. Native languages and tools (compilers, linkers, and so on) generates more platform specific and natural outputs (binaries). More platform specific binaries more performant apps, generally it means speed.

It also has disadvantages, such as:

  • We need to know three languages in order to develop the same app for the three platforms

  • We cannot share the business logic between different platforms and we need to develop and maintain three different business logics

  • The code of one platform is mostly not understandable to those who use the other platform

In big companies, the platform specific approach works because most of them have a lot of people dedicated to a project and probably they can afford three different teams. Each one of the teams usually works independently to develop the same app in each single platform. The iOS team cannot share a single line of code with the Android team nor the Windows Phone team, or vice versa. This is called the Silo approach.

People usually think about cross-platform mobile development as Write Once, Run Anywhere (WORA approach).

The main advantage of this approach is that we can write something that looks like a WebApp that runs everywhere. There are technologies such as Cordova, Titanium, and others based on WORA. They are all based on the lowest common denominator and can be extendable with plugins to support platform specific features such as NFC or Force Touch, and others.

We generally cannot take full advantage of the features offered by each platform.

Platform-specific features are mostly related to the capabilities of the OS installed and the hardware available in the device. They impact the overall user experience.

From Mono to Xamarin

Mono is an open source implementation of the Microsoft .NET Framework based on the Common Language Runtime (CLR).

It was initially released in 2004 after three years of open source development launched by a small team of people that included the current founder and CEO of Xamarin, Miguel De Icaza.

Note

Xamarin is derived from Mono framework, which is a cross-platform implementation of .Net Runtime. Xamarin was acquired by Microsoft and open sourced all Xamarin frameworks in February 2016. Microsoft also open sourced .Net Framework and made a cross-platform implementation of it, called .Net Core Framework.

MonoTouch was initially released in 2009 and in 2013 its name became Xamarin.iOS.

Xamarin.iOS is a set of libraries (.dll files) that bind the native iOS SDK.

The iOS binding is the way Xamarin maps the idioms used in Objective-C to the idioms used in .NET.

Mono for Android was initially released in 2011 and in 2013 its name became Xamarin.Android. It is a set of libraries that bind the native Android SDK APIs.

The Android binding maps the idioms used in Java to the idioms used in .NET.

Xamarin.iOS and Xamarin.Android are extendable.

A binding project can be written in order to wrap libraries written in Java or Objective-C into a dll that can be used from our C# projects.

Each single feature of the operating system, third-party libraries, and even our own native libraries can be ported writing a binding project.

The architecture of Xamarin allows us to use the best that iOS SDK can offer to iOS based-devices and the best that Android SDK can offer to Android-based devices.

Xamarin adds another value from the developers' point of view: it allows us to use most of the features of the .NET Framework while developing for Android and iOS devices.

From Xamarin to native

Xamarin.iOS based C# code is compiled using ahead-of-time (AOT) compilation. The resulting compilation output produces a single statically compiled ARM binary.

AOT compilation pre-generates all the native code that the Just in Time (JIT) compiler would normally generate from the Intermediate Language (IL). IL is stripped from the managed assemblies, leaving only metadata. AOT links the metadata together with the JIT-less runtime into a single native binary that can be signed with the Apple account.

Note

An Apple Developer account is needed in order to publish the application to the App Store for iOS devices.

For more information, visit https://developer.apple.com .

The Xamarin.Android application runs within the Mono execution environment (also known as Mono Virtual Machine). The Mono Virtual Machine runs side-by-side with the Dalvik Virtual Machine. Dalvik is an integral part of the Android software stack. It is a process virtual machine in the Android operating system that executes applications written for Android.

The connections between the two virtual machines are created by two Java Native Interface (JNI) bridges:

  • Android callable wrappers: These are used any time the Android runtime needs to invoke managed code

  • Managed callable wrappers: These are used any time the managed code needs to invoke Android code

The managed callable wrappers are generated via .jar binding and are responsible for converting between managed and Android types.

Xamarin cross-platform

With Xamarin we have different ways to share code.

In general, we want to create a platform specific app writing the most common code we can.

As we can see in the following image, Xamarin.Forms provides shared app logic and shared UI code across platforms. With that support, we can easily develop one true application running and displaying the same on all the platforms.

But there is a thin layer on top of shared layers, and that thin layer adds platform-specific customizations to the Xamarin.Forms projects:

The ways to share the common code are:

  • Portable Class Libraries

  • Shared Projects

Shared Projects work in a similar way to file linking. All the files contained in a Shared Project are conceptually copied into each referencing project and compiled as part of them.

Portable Class Libraries (PCL) allow us to produce libraries that can be shared across multiple platforms including Xamarin.iOS, Xamarin.Android, and Windows Phone.

When we develop a PCL, we can reference and use it in each platform specific project.

Xamarin.Forms is a UI Toolkit that allows us to share most of the UI components across platforms. Xamarin.Forms has been implemented as a .NET PCL.

What do we need to start?

To develop iOS Apps, we need a Mac in order to compile and run the code.

Xamarin and Microsoft both provide us with very useful and productive IDEs. Microsoft offers a state of the art IDE, Visual Studio, and its free edition Visual Studio Community to us to create cross-platform applications.

Xamarin offers the free Xamarin Studio to us to create cross-platform applications.

Microsoft's Visual Studio runs on Windows, while Xamarin's Xamarin Studio runs on Windows, Linux, and Mac OS.

We also need an App Store account that allows us to download the iOS SDK and Xcode. To develop iOS applications using C#, we need to download and install onto our Mac both Xamarin Studio and Xamarin.iOS.

We will learn more about Xamarin Studio later in this chapter.

To develop Android apps, we need to install the Android SDK.

We can access the Android SDK Manager directly from Xamarin Studio as well as Visual Studio.

To develop Android apps, we need either a Mac or a Windows machine with Xamarin Studio or Visual Studio with the Xamarin.Android plugin.