Introduction to Portable Class Library (PCL)

Introduction

In .Net we create a Class Library to reuse our code or make our code reusable with the same type of application only, but that might not work for some conditions. Assume you have created a class library for WPF or any other type of application. Then if you want to access the same class library for a Windows 8 or Windows Phone application then in this scenario you can’t, this is referred to as a class library.

Portable Class Library

Time is changing, people want the same application on all platforms to make life easy. An example is if you are working in an Office then probably you use a Desktop application or web application. If you are traveling then at that time you use your tab or mobile app. In other words, the same application is used on multiple platforms. Microsoft technology is growing day by day and it’s covered end to end with software for nearly every device, all types applications you can build and validate within one framework on all types of platforms, Windows, Web, Mobile or Tab.

PCL

What PortableClassLibrary In .Net 

The problem I explained above can be solved by the Portable Class Library(PCL). .Net 4.5 introduced the Portable Class Library (PCL) for developing your business logic in one location and you can use this library end to end in all types of applications or all platforms (Windows, web or Mobile) on the .Net Framework. In a single word, the Portable Class Library (PCL) allows the targeting of a set of platforms. For example WPF is a Windows application, it’s a Desktop based application. Windows Phone is a mobile application, it runs inside Windows Phone, if you refer to the same class library for WPF and Windows Phone then you will get the some error, because WPF has a different dependency and Windows Phone has a different dependency.

Above I defined the Portable Class Library (PCL) flow. All these applications run on the .Net framework but a different environment and platform but they all can easily use the Portable Class Library. In other words, you only need to prepare the UI and do the DataBinding of your application to be ready for all Windows, web and Mobile, because your same business logic can then use all platforms.

Supported platform by PortableClassLibrary

Platform Versions
.NET Framework .NET Framework 4
and higher .NET Framework 4.0.3 and higher
.NET Framework 4.5
Silverlight Silverlight 4 and higher (selected by default)
Silverlight 5
Windows Phone Windows Phone 7 and higher (selected by default) Windows Phone 7.5 and higher Windows Phone 8.

Above this platform support Portable Class Library, but note if want to work on window phone platform for that you need to first install the Windows Phone SDK for the Portable Class Library.

Features of Portable Class Library (PCL)

Feature .NET Framework Windows Store Silverlight Windows Phone Xbox 360
Core Yes          Yes Yes Yes Yes
LINQ Yes Yes Yes Yes  
IQueryable Yes Yes Yes 7.5 and higher  
Dynamic keyword Only 4.5 Yes Yes    
MEF Yes Yes Yes Yes  
NCL Yes Yes Yes Yes  
Serialization Yes Yes Yes Yes  
WCF Yes Yes Yes Yes  
MVVM Only 4.5 Yes Yes Yes  
Data annotations Only 4.0.3 and 4.5 Yes Yes    
XLINQ Only 4.0.3 and 4.5 Yes Yes Yes Yes
System.Numerics Yes   Yes Yes
 

Step-by-step one sample for Portable Class Library (PCL)

Step 1: Open Visual Studio then select "File" -> "New" -> "Project...".

Project in Visual Studio

Step 2: Select your Programing language, either C# or any other, I selected C# then search for  Portable Class Library.

Select Portable Class Library

Step 3:
Then the following Window will appear; select whatever you need to, or if you want some other library then you need to click "Install additional frameworks" and for that you need an internet connection.

Add Portable Class Library in Project

How To Refer to the Portable Class Library (PCL) in a Project

After building your PCL library you can add this reference in your project. Here you can refer to the project or specific assemblies that contain the classes you want to access. To run an app that references a Portable Class Library assembly, the required version (or later) of the targeted platforms must be installed on the computer. Visual Studio 2012 contains all the required frameworks, so you can run the app without further modification on the computer that you used to develop the app.

Deployment of Portable Class Library (PCL)

At the time of deployment you must check the dependency for the correct version of the .NET Framework. By specifying this dependency, you need to ensure that the required version is installed with your app. If you target the .NET Framework 4 or later then the computer must have the .NET Framework 4, Update 4.0.3 for the .NET Framework 4, or the .NET Framework 4.5 installed.


Similar Articles