Cross-Platform Development with the .NET Framework
The Portable Class Library project in Visual Studio 2012 supports the cross-platform development of .NET Framework apps.
Use this project to write and build portable assemblies that work without modification on multiple platforms, such as Windows 7, Windows 8, Silverlight, Windows Phone, and Xbox 360. For example, you can create classes that contain shared business logic for desktop apps, Windows Store apps, and mobile apps, and you can then reference those classes from your apps.
The Portable Class Library project supports a subset of assemblies from the .NET Framework, Silverlight, .NET for Windows Store apps, Windows Phone, and Xbox 360, and provides a Visual Studio template that you can use to build assemblies that run without modification on these platforms. If you don't use a Portable Class Library project, you must target a single app type, and then manually rework the class library for other app types. With the Portable Class Library project, you can reduce the time and costs of developing and testing code by building portable assemblies that are shared across apps for different devices.
The features of the Portable Class Library are described in the following sections:
If you're using Visual Studio 2012 as your development environment, everything you need to develop a Portable Class Library project is already available to you.
To install the Portable Class Library tools on a build machine without installing Visual Studio 2012, download the Portable Library Tools, and save the download file (PortableLibraryTools.exe) on your computer. Run the installation program from a Command Prompt window, and include the /buildmachine switch on the command line.
When you create a Portable Class Library project, you can choose two or more platforms you want to target. The following table shows the available platforms.
|
Platform |
Versions |
||
|---|---|---|---|
|
.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) Silverlight 5 |
||
|
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 |
||
|
Xbox 360 |
N/A |
The table identifies the four platforms or versions that are selected by default. You can change these default platforms by using the Project Properties dialog box, as shown in the section Selecting the Platforms to Target.
When you specify the platforms you want to target in a Portable Class Library project, the supported assemblies for those platforms are automatically referenced in your project. You do not have to add or remove assemblies. The referenced assemblies are automatically updated if you change the target platforms.
If you target only the .NET Framework 4.5 and the .NET for Windows Store apps, you have access to a much larger set of assemblies than is available in other platform combinations. This larger set of assemblies is almost identical to the .NET for Windows Store apps, but doesn't include the classes in the Windows.UI.Xaml namespaces. For more information, see .NET for Windows Store apps – supported APIs in the Windows Dev Center.
The following table shows which features are supported on the available platforms and versions.
|
Feature |
.NET Framework |
Windows Store |
Silverlight |
Windows Phone |
Xbox 360 |
|---|---|---|---|---|---|
|
Core |
√ |
√ |
√ |
√ |
√ |
|
LINQ |
√ |
√ |
√ |
√ |
|
|
IQueryable |
√ |
√ |
√ |
7.5 and higher |
|
|
Dynamic keyword |
Only 4.5 |
√ |
√ |
|
|
|
Managed Extensibility Framework (MEF) |
√ |
√ |
√ |
|
|
|
Network Class Library (NCL) |
√ |
√ |
√ |
√ |
|
|
Serialization |
√ |
√ |
√ |
√ |
|
|
Windows Communication Foundation (WCF) |
√ |
√ |
√ |
√ |
|
|
Model-View-View Model (MVVM) |
Only 4.5 |
√ |
√ |
√ |
|
|
Data annotations |
Only 4.0.3 and 4.5 |
√ |
√ |
|
|
|
XLINQ |
Only 4.0.3 and 4.5 |
√ |
√ |
√ |
√ |
|
System.Numerics |
√ |
√ |
√ |
|
|
The types and members that are available in Portable Class Library projects are constrained by several compatibility factors:
-
They must be shared across the target platforms you selected.
-
The must behave similarly across those platforms.
-
They must not be candidates for deprecation.
-
They must make sense in a portable environment, especially when supporting members are not portable.
For example, the Portable Class Library project doesn't contain any UI-related types or members because of behavioral differences between the UIs of different devices. You may also encounter limitations if you target platforms (such as Xbox, the .NET Framework 4, and Windows Phone 7) that were released before the introduction of the Portable Class Library.
You can find which members are supported by the Portable Class Library in the reference topics for the .NET Framework Class Library. In the members table for a class, the following Portable Class Library icon appears next to supported members.
For example, the following image shows that the Chars property in the String class is supported in the Portable Class Library.
You can also look in the Version Information section of a reference topic for a note indicating that a type or member is supported in the Portable Class Library project, as shown below.
Frequently, you will want to call non-portable members from a portable class. You cannot call non-portable members directly, because those members are not available in the Portable Class Library project. Instead, you can create an abstract class in the Portable Class Library project and use that class from all portable code. In the platform-specific project, you create a child class of the abstract class and implement platform-specific behaviors.
The following example shows a portable class for working with local settings. Local settings are handled differently in a Windows Store app and a Silverlight app, so the portable class doesn't provide any implementation details.
In your Portable Class Library project, you can use this method as follows.
In your Silverlight project, you add a reference to the portable assembly, and then create a child class that implements the local setting operation that is specific to a Silverlight environment. The following example shows a Silverlight implementation of the ExampleLocalSettings class.
In your Windows Store app, you add a reference to the portable assembly, and then create a child class that implements the local setting operation that is specific to Windows Store apps. The following example shows an implementation of the ExampleLocalSettings class for a Windows Store app.
In both the Silverlight app and the Windows Store app, you must initialize the specific implementation of the child class and set it to the Instance property. Typically, you create this instance when the app starts. The following example shows how to initialize the Silverlight implementation.
The following example shows how to initialize the implementation for the Windows Store app.
When you target the .NET Framework 4.5, .NET for Windows Store apps, Silverlight, and Windows Phone, you can implement the MVVM pattern in your solution. The classes to implement this pattern include the following:
-
System.Collections.ObjectModel.ReadOnlyObservableCollection(Of T)
-
System.Collections.Specialized.NotifyCollectionChangedAction
-
System.Collections.Specialized.NotifyCollectionChangedEventArgs
-
System.Collections.Specialized.NotifyCollectionChangedEventHandler
For more information about implementing the MVVM pattern in a Portable Class Library project, see Using Portable Class Library with Model-View-View Model.
By default, the Portable Class Library project targets the following platforms:
-
.NET Framework 4.5
-
Silverlight 4 and later
-
Windows Phone 7 and later
-
.NET for Windows Store apps
The project only references assemblies that are supported by those platforms. To change the target platforms, in Solution Explorer, open the shortcut menu for the Portable Class Library project, and then choose Properties.
On the project properties page, the Library tab specifies the platforms that are currently targeted.
To add or remove target platforms, choose the Change button, and then select the appropriate check boxes.
When you change the target platforms, the assemblies that are available in the project will change to match the set of assemblies that are supported for your selection. If your project references assemblies that are not supported by one of the platforms you selected, you must either remove the reference to the assembly or change the target platforms.
After you build your Portable Class Library project, you just reference it from other projects. You can reference either 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.
When you deploy a .NET Framework app that references a Portable Class Library assembly, you must specify a dependency on the correct version of the .NET Framework. By specifying this dependency, you ensure that the required version is installed with your app. If you target the .NET Framework 4 or later, the computer must have the .NET Framework 4 with an update, Update 4.0.3 for the .NET Framework 4, or the .NET Framework 4.5 installed.
-
To create a dependency with ClickOnce deployment: In Solution Explorer, choose the project node for the project you want to publish. (This is the project that references the Portable Class Library project.) On the menu bar, choose Project, Properties, and then choose the Publish tab. On the Publish page, choose Prerequisites. Select the required .NET Framework version (or .NET Framework 4 update) as a prerequisite.
-
To create a dependency with a setup project: In Solution Explorer, choose the setup project. On the menu bar, choose Project, Properties, Prerequisites. Select the required .NET Framework version as a prerequisite.
For more information about deploying .NET Framework apps, see .NET Framework Deployment Guide for Developers.
When you deploy a Silverlight-based app that references a Portable Class Library assembly, you must ensure that the minimum runtime version required for the app matches its targeted version. If you target Silverlight 4, the version must be 4.0.60129.0 or later. You set the version by including <param name="minRuntimeVersion" value="4.0.60129.0" /> in the webpage that hosts the Silverlight-based app, as follows:
<div id="silverlightControlHost"> <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> <param name="source" value="ClientBin/SilverlightApplication.xap"/> <param name="onError" value="onSilverlightError" /> <param name="background" value="white" /> <param name="minRuntimeVersion" value="4.0.60129.0" /> <param name="autoUpgrade" value="true" /> <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none"> <img src=http://go.microsoft.com/fwlink/?LinkId=161376 alt="Get Microsoft Silverlight" style="border-style:none"/> </a> </object> <iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"> </iframe> </div>
To make Portable Class Library assemblies compatible across all supported platforms, some members have been slightly changed in the Portable Class Library. For details about which members were changed and how they were changed, see API Differences in Portable Class Library.
Important