Working with portable class libraries

A Portable Class Library (PCL) is a C# library project that can be supported on multiple platforms including iOS, Android, Windows, Windows Store apps, Windows Phone, Silverlight, and Xbox 360. PCLs have been an effort by Microsoft to simplify development across different versions of the .NET framework. Xamarin has also added support for iOS and Android in the newer versions of their products. Many popular cross-platform frameworks and open source libraries are starting to develop PCL versions such as Json.NET and MVVMCross.

Let's set up a solution using PCLs. Before starting, make sure you at least have Mono 3.2.1 installed (look under Xamarin Studio | About Xamarin Studio | Show Details). At the time of writing this book, one could easily switch to the beta channel in Xamarin Studio to update to the version supporting PCLs.

Let's begin setting up our portable class library:

  1. Open Xamarin Studio and start a new solution.
  2. Select a new Portable Library project under the general C# section.
  3. Name the project ProductSearch.Core, and name the solution ProductSearch.
  4. Add the Product, ProductRepository, and ProductViewModel classes to the project used earlier in this chapter. You will need to add using System.Threading.Tasks; and using System.Linq; where needed.
  5. Click on Build | Build All from the menu at the top to be sure everything builds properly. You may wish to remove all references this project has set, as Xamarin Studio will display them with errors (this is a known issue with PCLs).
  6. Now, let's create a new iOS project by right-clicking on the solution and selecting Add | Add New Project. Create a new project by navigating to iOS | iPhone Storyboard | Single View Application and name it ProductSearch.iOS.
  7. Create a new Android project by right-clicking on the solution and selecting Add | Add New Project. Create a new project by navigating to Android | Android Application and name it ProductSearch.Droid.
  8. Simply add a reference to the portable class library from the iOS and Android projects.
  9. Click on Build | Build All from the top menu and you have set up a simple solution with a portable library.

Each solution type has their distinct advantages and disadvantages. PCLs are generally better, but there are certain cases where they can't be used. For example, if you were using a library such as MonoGame, which is a different library for each platform, you would be much better off using file linking or cloned project files. Similar issues would arise if you needed to use a preprocessor statement such as #if IPHONE or a native library such as the Facebook SDK on iOS or Android.

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

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