Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
From the author of Portable Class Libraries

Portable Class Libraries

Visual Studio 2012 introduces the portable class libraries, a special type of class libraries that use a subset of the .NET Framework that's common to WPF, Silverlight, Silverlight for Windows Phone 7.x, Xbox 360, and Windows Store apps. With portable class libraries, you write the code once; then your code can be shared across all the aforementioned platforms. Of course, this implies that only objects common to all platforms can be shared. More specifically, the following assemblies can be referenced in a portable class library:

  • MsCorlib.dll
  • System.dll
  • System.Core.dll
  • System.ComponentModel.Composition.dll
  • System.ComponentModel.Annotations.dll
  • System.Net.dll
  • System.Runtime.Serialization.dll
  • System.ServiceModel.dll
  • System.Xml.dll
  • System.Xml.Linq.dll
  • System.Xml.Serialization.dll
  • System.Windows.dll

Assemblies not included in this list are not considered portable. The MSDN Library has a more detailed list showing what can be shared.

To create a new portable class library, in Visual Studio 2012 select File > New > Project. In the New Project dialog box, select the Portable Class Library template, as shown in Figure 1.

Figure 1 Creating a new portable class library.

After clicking OK, select the list of platforms you want to target (the Xbox 360 is excluded by default), as shown in Figure 2. Notice that you can change the default selection; for example, you can choose .NET Framework 4.0.3 instead of 4.5, or Silverlight 5 instead of 4. Remember that changing the default selection can affect the list of objects that are shared, and some of them may no longer be available.

Figure 2 Selecting target platforms.

Once the project has been created, enable the Show All Files view in Solution Explorer. Notice that the list of references contains the item .NET Portable Subset, as shown in Figure 3. This subset exposes objects that are common to the selected target platforms. If you double-click this item, the Object Browser window will show in more detail the list of available namespaces and classes. Figure 4 shows an excerpt of the Object Browser.

Figure 3 The .NET Portable Subset holds references to shared assemblies.

Figure 4 The Object Browser shows details of the available namespaces and classes.

  • + Share This
  • 🔖 Save To Your Account