使用便携式类库进行跨平台开发Cross-platform development with the Portable Class Library

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

重要

因为可移植类库项目面向的.NET 实现仅非常特定的专项,我们强烈建议不要在新的应用程序开发中使用它们。Because Portable Class Library projects target only a very specific subset of .NET implementations, we strongly discourage their use in new application development. 推荐的替代是面向所有支持特定版本的.NET Standard 的.NET 实现的.NET Standard 库。The recommended replacement is a .NET Standard library, which targets all .NET implementations that support a specific version of the .NET Standard. 有关详细信息,请参阅 .NET StandardFor more information, see .NET Standard.

可移植类库可以帮助你减少开发和测试代码的时间和成本。Portable class libraries can help you reduce the time and costs of developing and testing code. 使用此项目类型可以编写和生成可移植的 .NET 框架程序集,然后从针对多个平台(如 .NET Framework、iOS 或 Mac)的应用引用这些程序集。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 the .NET Framework, iOS, or Mac.

即使是在 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 compiles your library with the new assemblies, which helps you identify the changes you need to make in your code.

创建便携式类库项目Create a Portable Class Library project

要创建便携式类库,请使用可视化工作室中提供的模板。To create a Portable Class Library, use the template provided in Visual Studio. 创建新项目 (文件 > 新项目),并在 "新项目"对话框中,选择您的编程语言(可视化 C# 或可视化基本)。Create a new project (File > New Project), and in the New Project dialog box, select your programming language (Visual C# or Visual Basic). 然后,选择类库(旧式可移植) 模板。Then, select the Class Library (Legacy Portable) template. 输入项目的名称,然后选择 "确定"。Enter a name for your project and choose OK.

将显示"添加便携式类库"对话框。The Add Portable Class Library dialog box appears. 选择两个或多个目标,然后选择 "确定"。Choose two or more targets, and then choose OK.

在可视化工作室中添加便携式类库目标

更改目标Change targets

您可以在创建便携式类库项目时或开始开发后更改其目标平台。You can change the target platforms of a portable class library project when you create it or after you've started development. 如果要在创建项目后更改目标,请在解决方案资源管理器中打开便携式类库项目的快捷方式菜单(不是解决方案),然后选择属性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.

可视化工作室中便携式类库的项目属性

要添加或删除目标,请选择 "更改" 按钮,然后选择并清除相应的复选框。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.

支持的类型和成员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.

如果一个成员在可移植类库和所选的目标中受支持,它将会你的 IntelliSense 项目中出现。If a member is supported in the Portable Class Library and for your selected targets, it will appear in your project in IntelliSense. 但是,请记住某一 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.

使用便携式类库Use 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 应用Deploy a Universal Windows app

当您创建引用便携式类库程序集的通用 Windows 应用时,部署该应用所需的一切内容都包含在应用包中,并且不需要执行其他步骤。When you create a Universal Windows 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 框架应用Deploy 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.

  • 要使用 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 版本作为系统必备组件。Select the required .NET Framework version 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 框架应用的详细信息,请参阅开发人员的部署指南For more information about deploying .NET Framework apps, see Deployment Guide for Developers.

另请参阅See also