Preparing for Multi-Platform Apps with Portable Class Libraries
This article introduces the Portable Class Library template that can help you write code that will run on multiple Windows platforms
The similarities between the app frameworks and the development environments for Windows Phone 8 and Windows 8 are very helpful if you develop for multiple platforms.
However in order to port an app to run on both platforms it is still necessary to redesign the XAML code and copy/paste the code-behind. Maintenance is also difficult - if you add a feature or fix a bug in one solution you have redo the same in the other solution, costing time and effort.
This article introduces a new project template called Portable Class Library that helps writing code for multiple platforms. This is present by default in Visual Studio 2012, and can be added as an extension to VS2010.
The functions in the portable parts of .Net Framework are mostly suited for implementing the data layer and the business logic of your apps. You will still need to port your XAML!
If there are classes missing in the PCL that are not available on all platforms the articles (see below) propose either using interfaces or abstract classes that need to be implemented for each platform. This way you can access features that are implemented differently on the diverse platforms.
The supported platforms are:
|.NET Framework|| .NET Framework 4 and higher
.NET Framework 4.0.3 and higher .NET Framework 4.5 (selected by Default)
|Silverlight|| Silverlight 4 and higher (selected by default)
|Windows Phone|| Windows Phone 7 and higher (selected by default)
Windows Phone 7.5 and higher
Windows Phone 8
|.NET for Windows Store apps||N/A|
- Note -> For Visual Studio 2010 users – and for Windows Phone 7 development - a Visual Studio "add-in" is available for download, as "Portable Library Tools 2", - at Visual Studio Gallery, The latest release version is 10.0.50828.
When creating a new Portable Class Library project, you can select the platforms your library shall support. This decision influences the classes and APIs that are available in the library.
Using a portable library for designing a WP7 app can efficiently simplify your work if you want to port to Windows 8. I hope the development of your app version for Windows Phone 8 can also gain from using a portable library for the kernel of your apps.
Using the PCL in a real project revealed that there are some limitations. For example I tried to use an ArgumentOutOfRangeException Constructor (String, Object, String) in a library I created for .Net 4.5, Silverlight, Win8 Store and Windows Phone 7.5. The MSDN documentation says this ctor is compatible with PCL but trying to use it in the project shows that Intellisense doesn't offer it. The ctor is not available in my project. I used another constructor as workaround. I bugged this in Microsoft Connect and got the response that it is too complex to document in MSDN and I should rely on Intellisense (Connect bug feedback). This happened with other classes and functions, too.
I found out that a Codeplex project is in the ways that tries to cover the missing parts in the Portable Class Libraries, PCL Contrib. Hopefully the limitations can be eliminated... There is no download available yet (Update: I missed it because there are no binaries, just source code: PCL Contrib Source Code). But the project coordinators (among them the main PCL developer David Kean of Microsoft) invite us to enter wishes for new library functions as new bug reports. We should do this and ask the missing parts from them!
Latest development: I asked Dave Kean, why there are Portable Class Libraries that are missing many functions and there is another project that fills in some oft the gaps, both from the same author. I got the reply yesterday. Dave wrote: " The biggest major reason is because this project covers APIs that we consider legacy or deprecated (such as BackgroundWorker). Shipping something in .NET implies that it will be supported, documented and serviced for as long the product itself is (which as it ships in Windows 8 is a long, long time)."
Hey, that's news for me! Even the latest 4.5 documentation (BackgroundWorker Class) doesn't say anything about deprecation.
Another new thing I learned from this reply was where to propose new features for Visual Studio. I always thought Connect was the platform for proposals but there is something else: UserVoice. He wrote: "What we choose to support, is entirely based on customer feedback, so if you have suggestions, please add them over visualstudio.uservoice.com.".
- (.ctor => parameterless constructor)
Portable Class Library Links
- Portable Library Tools 2 - Download for VS 2010
- Cross-Platform Development with the .NET Framework (MSDN)
- Using Portable Class Library with Model-View-View Model (MSDN)
- How to Make Portable Class Libraries Work for You(Blog posts)
- Base Class Library (BCL) (Blog)
- Sharing Code in .NET using Portable Class Library (pcl.codeplex.com)
- Creating a Continuous Client using PCLs (MSD Magazine)
- MvvmLight PCL (nuget)
- PCL Compliance Analyzer - Check your code portability!
- Portable Class Libraries Contrib Useful additions to Portable Class Libraries.