使用可移植类库的跨平台开发Cross-Platform Development with the Portable Class Library

Visual Studio 中 .NET Framework 可移植类库项目类型可帮助你快速轻松地为 Microsoft 平台构建跨平台应用和库。The .NET Framework Portable Class Library project type in Visual Studio helps you build cross-platform apps and libraries for Microsoft platforms quickly and easily.

重要

Because Portable Class Library projects target only a very specific subset of .NET implementations, we strongly discourage their use in new application development. The recommended replacement is a .NET Standard library, which targets all .NET implementations that support a specific version of the .NET Standard. For more information, see .NET Standard.

可移植类库可以帮助你减少开发和测试代码的时间和成本。Portable class libraries can help you reduce the time and costs of developing and testing code. 使用此项目类型编写和构建可移植 .NET Framework 程序集,然后从面向多个平台(如 Windows 和 Windows Phone)的应用,引用这些程序集。Use this project type to write and build portable .NET Framework assemblies, and then reference those assemblies from apps that target multiple platforms such as Windows and Windows Phone.

即使是在 Visual Studio 中创建可移植类库项目并开始开发它之后,你仍然可以更改目标平台。Even after you create a Portable Class Library project in Visual Studio and start developing it, you can change the target platforms. Visual Studio 将使用新的程序集编译你的库,这可帮助识别你需要在代码中进行的更改。Visual Studio will compile your library with the new assemblies, which helps you identify the changes you need to make in your code.

本文讨论了 Visual Studio 中的应用开发,但 Microsoft 还提供了可用于在其他工具(如 Xamarin)中开发应用和库的可移植类库引用程序集。This article discusses app development in Visual Studio, but Microsoft also provides Portable Class Library reference assemblies that you can use to develop apps and libraries with other tools such as Xamarin. 可以在非 Microsoft 平台的任何基于 .NET Framework 运行时上使用这些应用和库。You can use these apps and libraries on any .NET Framework-based runtime on non-Microsoft platforms. 有关引用程序集的详细信息,请参阅博客文章可移植类库 (PCL) 现已在所有平台上For more information about the reference assemblies, see the blog entry Portable Class Library (PCL) now available on all platforms. 若要下载该程序集,请参阅Microsoft.NET 可移植库引用程序集中从 Microsoft 下载中心获得。To download the assemblies, see Microsoft .NET Portable Library Reference Assemblies in the Microsoft Download Center. 有关如何通过 Xamarin 使用程序集的详细信息,请参阅博客文章PCL 和.NET NuGet 库现在已为 Xamarin 启用For more information about how to use the assemblies with Xamarin, see the blog entry PCL and .NET NuGet Libraries now enabled for Xamarin.

Visual Studio 提供了帮助你使用可移植类库进行开发的模板。Visual Studio provides templates to help you develop with the Portable Class Library. 根据你正在使用的 Visual Studio 版本,可用的模板和菜单可能会与本文中所述的有所不同。Depending on which version of Visual Studio you're using, available templates and menus may vary from those described in this article.

警告

Visual Studio 2013 Update 2 包括对可移植类库模板的更新。Visual Studio 2013 Update 2 includes updates to the Portable Class Library templates. 如果您具有早期版本的 Visual Studio 和 Visual Studio 2013 安装在同一台计算机上,然后再安装 Update 2 中,对更改目标框架选项将应用于这两个版本的 Visual Studio。If you have an earlier version of Visual Studio and Visual Studio 2013 installed on the same computer, and you then install Update 2, the changes to the Target Framework choices will be applied to both versions of Visual Studio.

本主题内容:In this topic:

Visual Studio 支持Visual Studio support
创建可移植类库项目Creating a Portable Class Library project
目标选项Target options
更改目标Changing targets
支持的功能Supported features
支持的类型和成员Supported types and members
可移植类库中的 API 差异API differences in the Portable Class Library
使用可移植类库Using the Portable Class Library

Visual Studio 支持Visual Studio support

对可移植类库的 Visual Studio 支持取决于你正在使用的 Visual Studio 的版本。Visual Studio support for the Portable Class Library depends on the version of Visual Studio you're using. 在某些情况下,你将拥有所需的一切,而在其他情况下,你将需要安装其他的项目,如下表所示。In some cases, you'll have everything you need, and in other cases, you'll need to install additional items, as shown in the following table.

Visual Studio SKUVisual Studio SKU 对创建可移植类库的支持Support for creating a Portable Class Library
Visual Studio 2010 专业版、高级专业版或旗舰版Visual Studio 2010, Professional, Premium, or Ultimate 是的当你安装可移植库工具Yes, when you install the Portable Library Tools.
Visual Studio Express 2010 版Visual Studio Express 2010 versions 不是。No.
Visual Studio 2012 专业版、高级专业版或旗舰版Visual Studio 2012 Professional, Premium, or Ultimate 可以。Yes. 对于 Windows Phone 8.0 支持安装Windows Phone SDK 8.0For Windows Phone 8.0 support, install the Windows Phone SDK 8.0.
Visual Studio Express 2012 版Visual Studio Express 2012 versions 不是。No.
Visual Studio 2013 专业版、高级专业版或旗舰版Visual Studio 2013 Professional, Premium, or Ultimate 可以。Yes. 对于 Windows Phone 8.1 支持,请安装最新版本的 Visual Studio 2013For Windows Phone 8.1 support, install the latest version of Visual Studio 2013.
Visual Studio Community 2013 for WindowsVisual Studio Community 2013 for Windows 是的当你安装最新版本的 Visual Studio Community 2013,其中包括 Update 2。Yes, when you install the latest version of Visual Studio Community 2013, which includes Update 2.

创建可移植类库项目Creating a Portable Class Library project

若要创建可移植类库,你应该使用 Visual Studio 中提供的模板之一。To create a Portable Class Library, you should use one of the templates provided in Visual Studio. 创建一个新项目,然后在新的项目对话框中的模板,选择目标语言 (C# 或 Visual Basic 中),并选择想要面向的平台之一。Create a new project, and in the New Project dialog box, under Templates, select your target language (C# or Visual Basic), and then select one of the platforms you want to target. 可在下一步选择附加的平台。You can select additional platforms in the next step.

在 Visual Studio 2013 Update 2,你可以选择类库 (可移植) 用于所选的语言和平台,以创建可移植类库模板。In Visual Studio 2013 Update 2, you can choose the Class Library (Portable) template for your chosen language and platform to create a Portable Class Library. 你将看到面向以下平台的此模板:You'll see this template for the following platforms:

  • 应用商店应用Store Apps

  • Windows 桌面Windows Desktop

  • SilverlightSilverlight

如果你想要在 C# 中创建面向 Windows Phone 8.1 和 Windows 8.1 的库,则可以选择应用商店应用,然后选择类库 (可移植的通用应用)If you want to create a library to target Windows Phone 8.1 and Windows 8.1 in C#, you can choose Store apps, and then choose Class Library (Portable for Universal Apps).

应用商店应用程序的可移植类库Portable Class Library for Store Apps

此模板自动选择 Windows 8.1 和 Windows Phone 8.1 作为目标。This template automatically selects Windows 8.1 and Windows Phone 8.1 as targets. 如果你创建的库仅针对 Windows Phone 8.1 或 Windows 8.1,则可以更改目标平台并在以后添加平台。If you create a library that targets only Windows Phone 8.1 or Windows 8.1, you can change the target platforms and add platforms later.

如果使用 Visual Studio 2012 或 Visual Studio 2013,而无需更新 2,创建一个新的项目并选择可移植类库下 Visual C# 或 Visual Basic 模板。If you're using Visual Studio 2012 or Visual Studio 2013 without Update 2, you create a new project and choose the Portable Class Library template under Visual C# or Visual Basic.

选择可移植库项目Select Portable Library project

添加可移植类库对话框随即出现,并可以选择附加的平台。The Add Portable Class Library dialog box appears, and you can select additional platforms. 该对话框将基于你所选的目标向你提供兼容性警告。The dialog box will give you compatibility warnings based on the targets you select.

更改 vs2013 的目标框架对话框Change target frameworks dialog for VS2013
为 Visual Studio 2013 Update 2 添加可移植类库对话框Add Portable Class Library dialog box for Visual Studio 2013 Update 2

无论是使用 Visual Studio 2012 还是使用 Visual Studio 2013,你都可以在创建可移植类库项目时选择平台,或在创建项目之后使用项目属性来修改目标平台。Regardless of whether you're using Visual Studio 2012 or Visual Studio 2013, you can select the platforms when you create a Portable Class Library project, or you can use the project properties to modify the target platforms after you create the project.

目标选项Target options

在创建可移植类库项目时,可以选择操作系统以及你想要针对的 NET Framework 版本。When you create a Portable Class Library project, you can choose the operating system and the .NET Framework version you want to target. 如果您使用的 Visual Studio 2013 和已安装 Update 2 或更高版本,可以选择类库 (可移植的通用应用) 模板来创建可移植类库面向 Windows 8.1 和 Windows Phone 8.1。If you're using Visual Studio 2013 and you've installed Update 2 or later, you can choose the Class Library (Portable for Universal Apps) template to create a Portable Class Library that targets Windows 8.1 and Windows Phone 8.1. 下表显示了可用的目标,具体取决于你使用的 Visual Studio 版本。The following table shows the available targets depending on the version of Visual Studio you're using.

目标选项Target option Visual Studio 2012Visual Studio 2012 Visual Studio 2013Visual Studio 2013 Visual Studio 2013 Update 2 或更高版本Visual Studio 2013 Update 2 or later
.NET Framework.NET Framework .NET framework 4 和更高版本- .NET Framework 4 and higher

.NET framework 4.0.3 和更高版本- .NET Framework 4.0.3 and higher

.NET framework 4.5- .NET Framework 4.5
.NET framework 4 和更高版本- .NET Framework 4 and higher

.NET framework 4.0.3 和更高版本- .NET Framework 4.0.3 and higher

.NET framework 4.5 和更高版本- .NET Framework 4.5 and higher

.NET framework 4.5.1- .NET Framework 4.5.1
.NET framework 4- .NET Framework 4

.NET framework 4.0.3- .NET Framework 4.0.3

.NET framework 4.5- .NET Framework 4.5

.NET framework 4.5.1- .NET Framework 4.5.1
Windows PhoneWindows Phone -Windows Phone 7 和更高版本- Windows Phone 7 and higher

-Windows Phone 7.5 和更高版本- Windows Phone 7.5 and higher

-Windows Phone 8- Windows Phone 8
-Windows Phone 8- Windows Phone 8 -Windows Phone Silverlight 8- Windows Phone Silverlight 8

-Windows Phone Silverlight 8.1- Windows Phone Silverlight 8.1

对于 Windows 运行时和 XAML 支持,请选择:For Windows Runtime and XAML support, choose:

-Windows Phone 8.1- Windows Phone 8.1
Windows 应用商店Windows Store -用于 Windows 应用商店应用的.NET- .NET For Windows Store Apps -Windows 应用商店应用 (Windows 8) 和更高版本- Windows Store Apps (Windows 8) and higher

-Windows 应用商店应用 (Windows 8.1)- Windows Store Apps (Windows 8.1)
Windows 8- Windows 8

-Windows 8.1- Windows 8.1
Silverlight- Silverlight Silverlight 4 和更高版本- Silverlight 4 and higher

Silverlight 5- Silverlight 5
Silverlight 5- Silverlight 5 Silverlight 5- Silverlight 5
XboxXbox -Xbox 360- Xbox 360 不可用N/A 不可用N/A

更改目标Changing targets

在你选择可移植类库模板时,已为你选择了默认平台,但这些默认平台将有所不同,具体取决于你安装的 Visual Studio 版本以及你以前选中的目标。When you choose a Portable Class Library template, default platforms are selected for you, but these defaults will vary depending on which version of Visual Studio you have installed, and which targets you have selected previously. 你可以在创建可移植类库时或在开始开发可移植类库后,更改这些平台。You can change the platforms when you create the Portable Class Library or after you’ve started development of a Portable Class Library.

如果你想要在创建你的项目后更改目标解决方案资源管理器,打开 (不是解决方案),你可移植类库项目的快捷菜单,然后选择属性.If you want to change the targets after you’ve created your project, in Solution Explorer, open the shortcut menu for your Portable Class Library project (not the solution), and then choose Properties. 在项目属性页上,选项卡将显示你的项目当前针对的平台。On the project properties page, the Library tab shows the platforms that your project currently targets.

项目属性Project properties
用于 Visual Studio 2013 Update 2 的可移植类库属性页Portable Class Library property page for Visual Studio 2013 Update 2

若要添加或删除的目标,请选择更改按钮,然后选择和清除相应的复选框。To add or remove targets, choose the Change button, and then select and clear the appropriate check boxes.

在你更改目标时,供你在开发项目时使用的 API 也将发生更改以匹配你的选择。When you change the targets, the APIs that are available to you for developing your project will change to match your selection. Visual Studio 报告因目标更改而可能导致的错误和警告。Visual Studio reports the errors and warnings that may occur as a result of the targets changing.

如果您想要评估可移植性的程序集之前在 Visual Studio 中进行更改,可以使用.NET 可移植性分析器If you want to evaluate the portability of your assemblies before you make changes in Visual Studio, you can use the .NET Portability Analyzer.

菜单选项将有所不同,具体取决于你使用的 Visual Studio 版本。The menu options will vary depending on the version of Visual Studio you're using.

更改目标Change target
更改 Visual Studio 2012 中的目标对话框Change Targets dialog box in Visual Studio 2012

支持的功能Supported features

下表显示了各可用平台和版本上支持的功能。The following table shows which features are supported on the available platforms and versions. 在某些情况下,Microsoft 已通过发布的 NuGet 包添加了支持功能,并且这已为用户所了解。In some cases, Microsoft has added support with the release of a NuGet package, and this has been noted. .NET Framework NuGet 包的相关详细信息,请参阅.NET Framework 和带外版本For more information about NuGet packages for the .NET Framework, see The .NET Framework and Out-of-Band Releases.

功能Feature .NET Framework.NET Framework .NET Framework.NET Framework .NET Framework.NET Framework Windows 应用商店Windows Store Windows 应用商店Windows Store Windows Phone 应用商店Windows Phone Store Windows Phone SilverlightWindows Phone Silverlight Windows Phone SilverlightWindows Phone Silverlight Windows Phone SilverlightWindows Phone Silverlight SilverlightSilverlight SilverlightSilverlight Xbox 360Xbox 360
44 4.0.34.0.3 4.54.5 88 8.18.1 8.18.1 7.57.5 88 8.18.1 44 55
核心库Core libraries
异步支持Async support
压缩Compression
数据注释Data annotations
动态关键字Dynamic keyword
HTTPClientHTTPClient
IQueryableIQueryable
语言集成查询 (LINQ)Language-Integrated Query (LINQ)
Managed Extensibility Network (MEF)Managed Extensibility Network (MEF)
网络类库 (NCL)Network Class Library (NCL)
序列化(数据协定、XML 和 JSON)Serialization (data contract, XML, and JSON)
System.NumericsSystem.Numerics
视图模型 (MVVM)View models (MVVM)
Windows Communication Foundation (WCF)Windows Communication Foundation (WCF)
Windows 运行时 APIWindows Runtime APIs
Windows.UI.XAMLWindows.UI.XAML
XLINQXLINQ

➊ 要求Microsoft Async➊ Requires Microsoft Async package
➋ 要求Microsoft 压缩➋ Requires Microsoft Compression package
➌ 要求Microsoft HTTP 客户端库➌ Requires Microsoft HTTP Client Libraries package

警告

引用时,可能会遇到错误Microsoft 压缩Microsoft HTTP 客户端库Windows Phone Silverlight 8.1 应用使用的可移植库中的包。You may encounter errors when you reference the Microsoft Compression and Microsoft HTTP Client Libraries packages from a portable library used by a Windows Phone Silverlight 8.1 app. 有关详细信息,请参阅平台兼容性和重大更改的 Windows Phone Silverlight 8.1 应用程序For more information, see Platform compatibility and breaking changes for Windows Phone Silverlight 8.1 apps.

支持的类型和成员Supported types and members

可移植类库项目中可用的类型和成员受若干兼容性因素的约束:The types and members that are available in Portable Class Library projects are constrained by several compatibility factors:

  • 它们必须在所选各目标之间共享。They must be shared across the targets you selected.

  • 它们必须在这些目标上具有类似的行为方式。The must behave similarly across those targets.

  • 它们不能是要弃用的候选项。They must not be candidates for deprecation.

  • 它们必须在可移植环境中有意义,特别是在支持成员不可移植时。They must make sense in a portable environment, especially when supporting members are not portable.

例如,只有当你针对 Windows 8.1 和 Windows Phone 8.1 时,可移植类库才会包含与 UI 相关的类型。For example, the Portable Class Library contains UI-related types only when you target Windows 8.1 and Windows Phone 8.1. 此外,如果针对的是在引入可移植类库之前发布的平台(如 Xbox、.NET Framework 4 和 Windows Phone 7),那么你可能会遇到一些限制。Also, you may 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. .NET Framework 通过 NuGet 发布的程序包,可改进对某些较旧版本的可移植类库支持。The .NET Framework releases packages through NuGet that improves the Portable Class Library support for some of these older platforms. 有关详细信息和 NuGet 包的列表,请参阅.NET Framework 和带外版本For more information and a list of NuGet packages, see The .NET Framework and Out-of-Band Releases.

如果一个成员在可移植类库和所选的目标中受支持,它将会你的 IntelliSense 项目中出现。If a member is supported in the Portable Class Library and for your selected targets, it will appear in your project in IntelliSense. 此外,可移植类库图标支持的可移植库成员表中显示在.NET Framework Class Library受支持成员旁边。In addition, the Portable Class Library icon Supported by Portable Library appears in the members tables, in the .NET Framework Class Library next to supported members. 例如,下面的成员表显示可移植类库支持 Chars[Int32] 类中的 String 属性:For example, the following members table shows that the Chars[Int32] property in the String class is supported in the Portable Class Library:

受支持的成员图标Supported Member icon
可移植类库图标Portable class library icon

您还可以查看版本信息部分下,该值的类型或成员是否支持可移植类库项目中的参考主题: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:

可移植库版本信息Portable Library Version Information
版本信息示例Version Information example

但是,请记住某一 API 可能在可移植类库中受支持,但你是否可以使用该 API ​​取决于你选择的目标。However, remember that an API may be supported in the Portable Class Library, but whether you can use the API depends on the targets you select.

可移植类库中的 API 差异API differences in the Portable Class Library

为了使可移植类库程序集在所有支持的平台中都兼容,可移植类库中对部分成员进行了轻微更改。To make Portable Class Library assemblies compatible across all supported platforms, some members have been slightly changed in the Portable Class Library.

使用可移植类库Using the Portable Class Library

构建可移植类库项目后,只需从其他项目中引用它即可。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 your computer. Visual Studio 包含所有必需的框架,因此你无需进一步修改即可在用来开发应用的计算机上运行该应用。Visual Studio contains all the required frameworks, so you can run the app without further modification on the computer that you used to develop the app.

部署 Windows 应用商店或 Windows Phone 应用Deploying a Windows Store or Windows Phone app

当你创建引用可移植类库程序集的 Windows 应用商店或 Windows Phone 应用时,部署该应用所需的一切皆包含在该应用包中,且无需执行进一步的步骤。When you create a Windows store or Windows Phone app that references a Portable Class Library assembly, everything you need to deploy the app is included in the app package, and no further steps are required.

部署 .NET Framework 应用Deploying a .NET Framework app

部署引用可移植类库程序集的 .NET Framework 应用时,你必须指定一个对 .NET Framework 正确版本的依赖项。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. 如果针对.NET Framework 4 或更高版本,计算机必须具有与在.NET Framework 4更新,.NET Framework 4 或.NET Framework 4.5 安装的更新 4.0.3。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.

  • 若要创建 ClickOnce 部署的依赖项: 在解决方案资源管理器,选择想要发布的项目的项目节点。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. 选择所需 .NET framework 版本(或 .NET Framework 4 更新)作为系统必备组件。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. 选择所需 .NET Framework 版本作为系统必备组件。Select the required .NET Framework version as a prerequisite.

有关部署.NET Framework 应用程序的详细信息,请参阅开发人员部署指南For more information about deploying .NET Framework apps, see Deployment Guide for Developers.

部署基于 Silverlight 的应用Deploying a Silverlight-based app

当部署引用可移植类库程序集的基于 Silverlight 的应用时,必须确保该应用所需的最低运行时版本与其目标版本相匹配。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. 如果针对 Silverlight 4,则版本必须是 4.0.60129.0 或更高版本。If you target Silverlight 4, the version must be 4.0.60129.0 or later. 你可以在承载基于 Silverlight 的应用的网页中包括 <param name="minRuntimeVersion" value="4.0.60129.0" />,从而设置版本(如下所示):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="https://www.microsoft.com/getsilverlight/get-started/install/"   
             style="text-decoration:none">  
      <img src=http://download.microsoft.com/download/5/1/6/5165823D-1D79-4871-8AC2-42DDDB94A5C2/PNGs/SLMedallion_ENU.png  
             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>  

请参阅See Also

将可移植类库与 MVVM 配合使用Using Portable Class Library with MVVM
面向多个平台的库的应用资源App Resources for Libraries That Target Multiple Platforms
.NET 可移植性分析器.NET Portability Analyzer
.NET Framework 对 Windows 应用商店应用和 Windows 运行时的支持情况.NET Framework Support for Windows Store Apps and Windows Runtime
部署Deployment