Cross Platform Code Sharing – Part 3 – PCLs

Cross Platform Code Sharing – Part 3 – PCLs

We’re on to part 2 of the ongoing series of sharing code across platforms using Xamarin.  The first part covered the why and when to share code, part number 2 was about Shared Projects, and today we’ll cover the final way to share code across platforms. If you remember, there are 3 ways to share code across platforms: Linked Files Shared Projects Portable Class Libraries We’re not going to bother with Linked Files, since those are falling out of favor.  Part number 2 covered Shared Projects.  So last, but certainly not least, let’s talk about Portable Class Libraries, or PCLs. What are Portable Class Libraries?   First off, Portable Class Library is way too long to type, so we’re going to call them PCLs from here on out.  Now that we have that established, let’s answer the question of what exactly is a PCL? A PCL is a .NET library which targets a known set of features of the Base Class Library (BCL), and can be used across platforms which support that known feature set, called Profiles, without recompiling. This means that unlike a Shared Library, a PCL actually creates a DLL, and that DLL can then be added to any project in the same way that it would normally reference a DLL.  The key difference however, is that the referencing project which the PCL DLL is being added to needs to support the same or a more verbose profile than the PCL (a profile is simply a designation of which features of the BCL the PCL has access to). Simply: A PCL can be used across platforms without recompiling!! Another way in which PCLs differ from Shared...
Cross Platform Code Sharing – Part 2 – Shared Projects

Cross Platform Code Sharing – Part 2 – Shared Projects

Welcome back to part 2 of the ongoing saga of sharing code across platforms using Xamarin.  The last time we talked about the why and when to share code, today we’ll cover one of the ways of how to share code. Unfortunately we won’t be sharing pizza today, but there are 3 ways to share code across projects: Linked files Shared Projects Portable Class Libraries Linked files are going the way of the dodo and generally are not used any longer and are being replaced in favor of Shared Projects.  So let’s investigate how to share code using “Shared Projects”, and in the next post in this series we’ll investigate Portable Class Libraries, or PCLs. What are Shared Projects? A good place to start with is defining exactly what is meant by a Shared Project.  To quote Xamarin: Shared Projects let you write common code that is referenced by a number of different application projects. The code is compiled as part of each referencing project and can include compiler directives to help incorporate platform-specific functionality into the shared code base. In other words, Shared Projects are a project type which get compiled directly into the project referencing them, and you can even use platform specific APIs within them – which are identified by preprocessor directives.  That means you can use specific iOS, Android or Windows Phone APIs within a Shared Project and the preprocessor directive ignores anything not a part of the platform you’re compiling for.  Under the hood, Visual Studio and Xamarin Studio treat Shared Projects the same way they treat linked files. Shared Projects were introduced in Visual Studio 2013 R2...
Intro To Cross Platform Code Sharing With Xamarin – Part 1

Intro To Cross Platform Code Sharing With Xamarin – Part 1

Wow – how’s that for the longest title ever for a blog post? What is “Code Sharing”? When I say the words “code sharing”, I’m talking about creating a library that can be used across all platforms that Xamarin supports (and even some they do not).  In other words, a single library that can be shared across an iOS, Android, Windows Phone, and an OS X application.  To make things even better, when properly done, we don’t need to do anything special to share the code across the platforms.  All we need to do is import the library into the solution and it will work.  There’s no need to write code and compile especially for Android and then turn around and make code changes and recompile for iOS – we just need to write and compile the library once!  That’s pretty cool – no wonder I acted like this guy when I found out about this! When to Code Share? There are certain scenarios where code sharing across platforms is appropriate (and useful) and others that sharing code does not make sense.  Let’s start with the scenarios where we’ll get the most bang for our buck with code sharing. Business Logic This is business logic in the traditional sense of the term, for example, model classes that represent the core entities of your application.  The classic example being a customer class that models the name and address of a customer.  Then any manipulations to those models, like a customer class being able to place an order, are part of the business logic that can be shared across platforms. REST/Web Services This one is really pretty cool....