dcsimg

Xamarin, Part 5: Portable Class Libraries (PCLs)

WEBINAR:
On-Demand

Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


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.



About the Author

Hannes DuPreez

Hannes du Preez is an ex MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic and C#. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to 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