使用可移植类库的跨平台开发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 Framework 程序集,然后从面向.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

若要创建可移植类库,请使用 Visual Studio 中提供的模板。To create a Portable Class Library, use the template provided in Visual Studio. 创建新的项目 (文件 > 新建项目),然后在新建项目对话框框中,选择您的编程语言 (Visual C# 或 Visual Basic)。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.

在 Visual Studio 中添加可移植库目标

更改目标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.

在 Visual Studio 中的可移植类库的项目属性

若要添加或删除的目标,请选择更改按钮,然后选择和清除相应的复选框。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 Framework 应用程序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 Framework 应用程序的详细信息,请参阅开发人员部署指南For more information about deploying .NET Framework apps, see Deployment Guide for Developers.

请参阅See also