Visual Studio contains a template for projects that need to share code across multiple software frameworks. This is known as a Portable Class Library or PCL. This tutorial shows how to create a Portable Class Library and configure which target frameworks it supports. You’ll see how to reference the PCL in your applications and how the Visual Studio compiler safeguards the development of these libraries, preventing you from unintentionally writing code that won’t work on the specified targets.
- [Voiceover] Making reusable code is a touchstone of software development. We even have a clever name for the symptoms that lead to refactoring repetitious code, don't repeat yourself, which is associated with the D-R-Y, or DRY, acronym. Here's a course on lynda.com that talks about this concept for WordPress. Really, the whole point of creating a Class Library project is reusability. As our apps spread across platforms and operating systems, we run into a dilemma. How do we make Class Libraries that work across these diverse systems? One answer is by creating a services layer, but I'll save that topic for later.
In this video, I'll talk about the concept known as Portable Class Libraries. My solution has two solution level folders, one for my Portable Class Libraries. I've got two of them in here, one that shows you what a default project looks like and the one that actually contains a code I'll use in the demo, and then in the Consumers folder, I have three projects. I have a WPF, or Windows Presentation Consumer. I have a Windows 10 Consumer, which is written to use on Universal Windows, and I have this Windows Phone 8 Consumer.
Let's take a look at the default PCL. Let's see what's in this project. This is what you get when you create a brand new project. There's not much here. There's a simple class. There is a series of references in this References folder, and if I double click on Properties, you'll see the Targeting screen, or the Targets screen at the bottom. This shows all of the frameworks that I consider valid for this Code Library. In other words, my code in this Library should be compatible with all of these targets and work across every one of these frameworks. There should be no calling an API that works in ASP.NET Core 5, but doesn't work in Windows 8.
Plus, any app based on these frameworks is eligible to use my Class Library. I can change these targets. I currently have about seven of them. I can change them by clicking on the Change button. There are a number of targets I can choose. These dropdowns have more. There are several versions of the .NET Framework, there are several versions of Windows, and there's two versions of the Windows Phone Silverlight framework. How do you create your own Portable Class Library? Move to the New Project dialogue, pick Class Library Portable, give your Library a name, then click on the OK button.
At this point, you'll see the same dialogue we saw a minute ago, the Add Portable Class Library. It's here that I pick the Libraries I want. As I'm adding the targets, you'll see across the bottom the error message down here says that if I use this set of targets, it'll make this project incompatible with Visual Studio 2013. If I add this item, you'll see that now I have a warning that says it's now incompatible with Visual Studio 2012. If I add Silverlight 5, it says this doesn't work across any of these at the moment, so we have to remove one of them.
I'll click on Cancel, then I'll click on Cancel. I've already created it in my PCL, so let's look at what we have. We'll start by looking at the code that I have in this NameGenPCL. I have two static methods inside this static class. I have a Get TourName, which returns a string, and I have this method here, which builds up this thing called a BigInteger. I will call some of these from my Consumers.
So, to add them to my Consumers, I need to go, first of all, I need to compile this to make sure this will compile. That's successful. All right. Now, I'll go to my Consumers, and I'll open up this WPF application, and I'll add a reference, and the way you add references in Visual Studio is you right click, you choose Add Reference, and then you can go several places. I'm going to go to the Solution node and check this check mark for NameGenPCL, just that simple. So, once I have the reference, then I can go to my code behind window here, for this main window, and down here in the button_Click handler, I'll make a call to that static method, get the TourName, and then assign that name to my textBlock.
Now, when I right click on this and make this my Startup Project and run the application, everything works. I'm making some calls to that code inside my PCL. I'll click on this button, and there's my string. Next, I'd like to see if I can make this work on a Windows 10 application. This is supposed to be able to work. Let's see if it does. We'll go to my Windows 10 app. Go to my References list and see that I already have a reference here to NameGenPCL, so that's looking good, and the code is essentially the same.
If I go over and look at the code in this MainPage.xaml for the Windows 10 application, you'll see that it's virtually identical to what I just showed you one minute ago. Because I'm working in Windows 10, it works across phones, and it works across desktop and tablets. So, when I go to build this application, I have to pick what target I want. How do I want to test it? So, let me show you what I mean. I'll right click on this to make this my Startup, and now in this dropdown here I can pick where I want to test deploy it.
So, I can use that on my local machine or on a device, or I can use these emulators. I thought you might like to see what it looks like on this phone emulator, so I picked this one here, this 720p 5 inch Mobile Emulator. Now, I can either click on this green triangle, or I can click on Debug, and start debugging. After a few seconds on my computer, up pops the Emulator, and my application is automatically loaded, and now I can click on the button and see that I'm making a successful call to my Library.
Now, I'm gonna go look at the Properties here. You notice that I have support for Windows Phone 8.1, but I do not have support for one of the older phone APIs, which is Windows Phone 8.0, but I have this PhoneConsumer that's targeting 8.0. So, what would be nice for me is if a developer who attempts to use my Library, and I don't support that framework, that they would know they can't do that, and that's exactly what Visual Studio does, and that's one of the prime reasons why we use Portable Class Libraries, because we get these warnings.
So, this Windows Phone developer comes in here and says, oh, I want to use this new Library I heard about. They go to the Reference, and they choose to add a Reference. They pick this item here and click on OK, and you get this dialogue, unable to add a reference to project NameGenPCL, and then it says the current project's target is not one of or compatible with the targets registered. It also tells me how to fix this. On some computers you may see this additional error dialogue, it is safe to ignore it and click OK.
Now I'll go back to my PCL, and I'll click on Change, and I'll choose to support Windows Phone 8. Now the developer can go back in here, and choose Add Reference, and they no longer get that error message. For my next demonstration, I'll remove this Reference, and we'll see if we can go the other way. Here's the other problem that we can have. As a developer that's writing the PCL, I could inadvertently make a call to some part of the API that doesn't work across all of the targets.
So, it'd be nice if I could get a warning from Visual Studio. So, right now let's go look at Name Generator. Here's an example. This BigInteger class is not usable in some of my targets, so because it's not supported in Windows 8.0, when I move into my code, you can see there's a red squiggle here. Now, unfortunately, the message doesn't say you can't use it because of the target error. It says the type or namespace Numerics does not exist in this namespace System, so the error message could be a little bit better, but it won't let me compile at the moment.
Yeah, it's giving me this failure here. Of course, the answer is I need to stop supporting the framework that doesn't work, and that's the Windows 8.1 I just added. So, I'll go back to my Properties, close that screen, then click on Change, stop supporting Windows Phone Silverlight 8. Now I wanna go back into my code inside my NameGenerator. You'll see that I no longer have the red squiggle under the Numerics namespace.
In closing, the Portable Class Library is a great way to build shareable libraries of code that gives me warnings if I try to consume the Library when it's not compatible and when I write code inside the Library that doesn't work across all of my target frameworks.
In this course, Walt Ritscher helps develop a sample app for each of the three platforms using Visual Studio and the IDEs in Xamarin (including Xamarin Studio). He touches on the processes that make Android, iOS, and Windows app development unique. Then he shows how to recreate the three apps as a single cross-platform app, and test and debug the app using Xamarin's powerful emulators. He also covers UI design with Xamarin Forms, the data binding engine that automatically syncs data between views and data sources, and pages, Xamarin's templates for content, navigation, tabbed, carousel, and master pages.
- What is Xamarin?
- Using Xamarin Forms and Xamarin Studio
- Setting up the development environment in Xamarin
- Touring the Xamarin IDEs
- Creating a new project in Visual Studio
- Creating the app UI
- Debugging the app on an emulator
- Creating a code library for the app
- Understanding the elements of a Xamarin cross-platform app
- Define UI with markup in Xamarin Forms
- Understanding data binding
- Exploring pages and the five page types