Xamarin, Part 5: Portable Class Libraries (PCLs)


Welcome to the final installment of my Xamarin article series. I think you'll agree that we have covered a lot during the past few months, and I hope that this series has helped you in some way. The last thing I'd like to talk about is PCLs (Portable Class Libraries), and today you will learn about them and what they do.

Portable Class Libraries

A Portable Class Library (PCL) can be supported on multiple platforms, such as:

  • iOS
  • Android
  • Windows
  • Windows Phone
  • Windows Store
  • Silverlight
  • Xbox 360

PCLs simplify development across different platform-specific projects.

Why is this important?

Building cross-platform applications used to be quite difficult. Now, with the use of PCLs, we can share code across various platform-specific projects. Remember, different platforms use different sub-sets of the .NET Base Class Library (BCL), meaning that they are in fact built to a different .NET Core Library Profile. That meant that each platform could only use class libraries that were targeted to that same profile.

Upon creating a Portable Class Library, we can choose a combination of platforms we want our code to run on. The compatibility choices you make upon creating a PCL are translated into a Profile that describes which platforms the library supports.

Creating a PCL

To create a new library that can be shared on various platforms, use the following steps:

  1. Start Visual Studio.
  2. Click File.
  3. Select New, then Project.
  4. Select the Class Library (Legacy Portable), as shown in Figure 1.

    Class Library
    Figure 1: Class Library

  5. Click OK.
  6. The screen shown in Figure 2 will appear.

    Figure 2: Profile

  7. Select All of the checkboxes; then, click OK.
  8. Notice the project name in the Solution Explorer: It includes the word Portable.

This project we have created now will be able to run on all the platforms we selected earlier. This is a Class Library, so it doesn't have a User Interface; we need to set a reference to this project wherever we want to use it.

Add a Reference by Selecting, Project, Add Reference, and select the project (see Figure 3).

Reference Manager
Figure 3: Reference Manager


This concludes my Xamarin series. I hope that you have enjoyed it and benefited from it.

This article was originally published on June 28th, 2019

About the Author

Hannes DuPreez

Hannes du Preez is a self-taught developer who started learning to program in the days of QBasic. He has written several articles over the years detailing his programming quests and adventures. .NET is his second love, just after his wife and kid. He has always been an avid supporter of .NET since the beginning and is an expert in VB and C#. He was given the Microsoft Most Valuable Professional Award for .NET (2008–2017). He has worked as a moderator and an article reviewer on online forums and currently works as a C# developer and writes articles for CodeGuru.com, Developer.com, DevX.com, and the Database journal.
His first book Visual Studio 2019 In Depth is currently on sale on Amazon and Bpb Publications.

You could reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date