Reader small image

You're reading from  Xamarin 4.x Cross-Platform Application Development - Third Edition

Product typeBook
Published inDec 2016
Reading LevelIntermediate
Publisher
ISBN-139781786465412
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
Jonathan Peppers
Jonathan Peppers
author image
Jonathan Peppers

Jonathan Peppers is a Xamarin MVP and lead developer on popular apps and games at Hitcents such as the Hanx Writer (for Tom Hanks) and the Draw a Stickman franchise. Jon has been working with C# for over 10 years working on a wide range of projects at Hitcents. Jon began his career working Self-Checkout software written in WinForms and later migrated to WPF. Over his career, he has worked with many .NET-centric technologies such as ASP.Net WebForms, MVC, Windows Azure, WinRT/UWP, F#, and Unity3D. In recent years, Hitcents has been heavily investing in mobile development with Xamarin, and has development over 50 mobile applications across multiple platforms.
Read more about Jonathan Peppers

Right arrow

Chapter 10. Third-Party Libraries

Xamarin supports a subset of the .NET framework, but for the most part includes all the standard APIs you would expect in the .NET base class libraries. Because of this, a large portion of C# open-source libraries can be used directly in Xamarin projects. Additionally, if an open source project doesn't have a Xamarin or portable class library version, porting the code to be used in a Xamarin project can often be very straightforward. Xamarin also supports calling native Objective-C and Java libraries, so we will explore these as additional means of reusing existing code.

In this chapter, we will cover the following:

  • The Xamarin Component Store

  • Porting existing C# libraries

  • Objective-C bindings

  • Java bindings

The Xamarin Component Store


The primary and obvious way to add third-party components to your project is via the Xamarin Component Store. The Component Store is fairly similar to the NuGet package manager that all C# developers are familiar with, except that the Component Store also contains premium components that are not free. All Xamarin components are also required to include full sample projects and a Getting Started guide, while NuGet does not inherently provide documentation in its packages.

All Xamarin.iOS and Xamarin.Android projects come with a Components folder. To get started, simply right-click on the folder, and select Get More Components to launch the store dialog, shown in the following screenshot:

At the time of writing this book, there are well over 200 components available to enhance your iOS and Android applications. This is a great place to find the most common components to use within your Xamarin applications. Each component is complete with artwork, possibly a demonstration...

Porting existing C# libraries


Even though Xamarin is becoming a popular platform, many open-source .NET libraries are simply not up to speed with supporting Xamarin.iOS and Xamarin.Android. But in these cases, you are definitely not out of luck. Often, if there is a Silverlight or Windows Phone version of the library, you can simply create an iOS or Android class library and add the files with no code changes.

To illustrate this process, let's port an open source project that doesn't have Xamarin or portable class library support. I have selected a dependency injection library called Ninject, due to its usefulness and relationship to ninjas. Find out more about the library at http://www.ninject.org/.

Let's begin setting up the library to work with Xamarin projects as follows:

  1. First, download the source code for Ninject from https://github.com/ninject/ninject.

  2. Create a new solution with an iOS Class Library project named Ninject.iOS.

  3. Link in all the files from the Ninject main project. Make sure...

Objective-C bindings


Xamarin has developed a sophisticated system for calling native Objective-C libraries from C# in iOS projects. The core of Xamarin.iOS uses this same technology to call native Apple APIs in UIKit, CoreGraphics, and other iOS frameworks. Developers can create iOS binding projects to expose Objective-C classes and methods to C# using simple interfaces and attributes.

To aid in creating Objective-C bindings, Xamarin has created a small tool named Objective Sharpie that can process Objective-C header files for you and export the valid C# definitions to add to a binding project. This tool is a great starting point for most bindings and will get about 75% of your binding project working in most cases. You will want to hand-edit and fine-tune things to be more C#-friendly most of the time.

Tip

Note that iOS binding projects can be created in Visual Studio; however, Objective Sharpie is a command-line tool for OS X. It leverages tooling included with Xcode, so iOS binding development...

Java bindings


In the same manner as iOS, Xamarin has provided full support for calling into Java libraries from C# with Xamarin.Android. The native Android SDKs function in this way and developers can leverage the Android Java Bindings project to take advantage of other native Java libraries in C#. The main difference here is that not a lot has to be done by hand in comparison to Objective-C bindings. The Java syntax is very similar to that of C#, so many mappings are exactly one-to-one. In addition, Java has metadata information included with its libraries, which Xamarin uses to automatically generate the C# code required for calling into Java.

As an example, let's make a binding for the Android version of the Google Analytics SDK. Before we begin, download the SDK at https://developers.google.com/analytics/devguides/collection/android/v3/. At the time of writing, Google Analytics is being migrated to Google Play Services, but we will use this Java library as an exercise for creating Java...

Using XPath in Java bindings


So, before we get started on solving these issues in our Java binding, let's first clean up the namespaces in the project. Java namespaces are in the form com.mycompany.mylibrary by default, so let's change the definition to match C# more closely. In the Transforms directory of the project, open Metadata.xml and add the following XML tag inside the root metadata node:

<attr path="/api/package[@name='com.google.analytics.tracking   
  .android']" name="managedName">GoogleAnalytics.Tracking</attr> 

The attr node tells the Xamarin compiler what needs to be replaced, in the Java definition, with another value. In this case, we are replacing managedName of the package with GoogleAnalytics.Tracking because it will make much more sense in C#. The path value may look a bit strange, which is because it is using an XML matching query language named XPath. In general, just think of it as a pattern matching query for XML. For full documentation on XPath...

Summary


In this chapter, we added libraries from the Xamarin Component Store to Xamarin projects and ported an existing C# library, Ninject, to both Xamarin.iOS and Xamarin.Android. Next, we installed Objective Sharpie and explored its usage for generating Objective-C bindings. Finally, we wrote a functional Objective-C binding for the Google Analytics SDK for iOS and a Java binding for the Google Analytics SDK for Android. We also wrote several XPath expressions to clean up the Java binding.

There are several available options for using existing third-party libraries from your Xamarin.iOS and Xamarin.Android applications. We looked at everything from using the Xamarin Component Store, porting existing code, and setting up Java and Objective-C libraries to be used from C#. In the next chapter, we will cover the Xamarin.Mobile library as a way to access a user's contacts, camera, and GPS location.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Xamarin 4.x Cross-Platform Application Development - Third Edition
Published in: Dec 2016Publisher: ISBN-13: 9781786465412
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jonathan Peppers

Jonathan Peppers is a Xamarin MVP and lead developer on popular apps and games at Hitcents such as the Hanx Writer (for Tom Hanks) and the Draw a Stickman franchise. Jon has been working with C# for over 10 years working on a wide range of projects at Hitcents. Jon began his career working Self-Checkout software written in WinForms and later migrated to WPF. Over his career, he has worked with many .NET-centric technologies such as ASP.Net WebForms, MVC, Windows Azure, WinRT/UWP, F#, and Unity3D. In recent years, Hitcents has been heavily investing in mobile development with Xamarin, and has development over 50 mobile applications across multiple platforms.
Read more about Jonathan Peppers