Xamarin, Part 5: Portable Class Libraries (PCLs)

Introduction

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.

    Profile
    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

Conclusion

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

Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Must Read