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. Many times, 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 help with this process, Xamarin has created an online service tool for scanning your existing code and determining how far off a library is from being portable. Navigate to http://scan.xamarin.com and upload any *.exe or *.dll file to have its methods analyzed for cross-platform development. After the scanning process, you'll get a report of the percentage of how much your component or application is portable to all platforms (Android, iOS, Windows Phone, and Windows Store).

The following screenshot is a sample report of the SignalR .NET Client library:

Porting existing C# libraries

If the library is running a high percentage on portability, you should have a relatively easy time porting it to Android or iOS. In most cases, it can even be easier to port the library to Xamarin than Windows Phone or WinRT.

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. Open Ninject.sln in Xamarin Studio.
  3. Note that the solution does not compile under Mono in Xamarin Studio. That is okay, since we can get it to work for iOS and Android. Ninject has primarily been developed for .NET on Windows and doesn't target Mono on the Mac.
  4. Add a new iOS Library Project named Ninject.iOS.
  5. Link in all the files from the Ninject main project. Make sure you use the Add Existing Folder dialog to speed up this process.

Tip

If you aren't familiar with GitHub, I recommend downloading the desktop client for Mac found at http://mac.github.com.

Now try to build the Ninject.iOS project; you will get several compiler errors in a file named DynamicMethodFactory.cs, as shown in the following screenshot:

Porting existing C# libraries

Open DynamicMethodFactory.cs and notice the following code at the top of the file:

#if !NO_LCG
#region Using Directives
  using System;
  using System.Reflection;
  using System.Reflection.Emit;
  using Ninject.Components;
#endregion

/// *** File contents here ***

#endif

It is not possible to use System.Reflection.Emit on iOS due to Apple's platform restrictions. Luckily, the library writers have created a preprocessor directive called NO_LCG (stands for Lightweight Code Generation), to allow the library to run on platforms that do not support System.Reflection.Emit.

To fix our iOS project, open the project options and navigate to the Build | Compiler section. Add NO_LCG to the Define Symbols field for both Debug and Release in the Configuration drop down. Click on OK to save your changes. Notice how the entire file is now highlighted with a light gray color in Xamarin Studio as shown in the following screenshot; this means the code will be omitted from being compiled:

Porting existing C# libraries

If you compile the project now, it will be completed successfully and a Ninject.iOS.dll file will be created, which you can reference from any Xamarin.iOS project. You can also reference the Ninject.iOS project directly instead of using the *.dll file.

At this point, you may wish to repeat the process to create a Xamarin.Android class library project. Luckily, Xamarin.Android supports System.Reflection.Emit, so you can skip adding the additional preprocessor directive if you wish.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.202.224