为 NuGet 创建新的多平台库Creating a New Multiplatform Library for NuGet

创建使用 PCL 或 .NET Standard 的多平台库项目意味着生成的 NuGet 可以添加到支持目标配置文件的任何 .NET 项目,包括 ASP.NET 项目,或使用 WinForms、WPF 或 UWP 的桌面应用程序。Creating a Multiplatform Library project that uses PCL or .NET Standard means that the resulting NuGet can be added to any .NET project that supports the target profile, including ASP.NET projects, or desktop apps using WinForms, WPF, or UWP.

库只能包含所选 PCL 或 .NET Standard 配置文件支持的代码,以及任何其他添加的 Nuget。The library can only contain code supported by the selected PCL or .NET Standard profile, as well as any other NuGets that are added. 这适用于可以完全用 .NET 基类库表达的业务逻辑和算法。This is suited to business logic and algorithms that can be expressed entirely in the .NET base class library.

创建一个程序集,并将其内置于 NuGet 包中。A single assembly is created and built into a NuGet package.

如果以后需要特定于平台的功能,则可以添加特定于平台的项目If you later need platform-specific functionality, platform-specific projects can be added.

创建多平台库 NuGet 的步骤Steps to Create a Multiplatform Library NuGet

  1. 选择 "文件" > "新建解决方案" (或者右键单击现有解决方案,然后选择 "添加 >" 新建项目")。Select File > New Solution (or right click an existing solution and choose Add > New Project).

  2. 从多平台 > 库部分中选择多平台库Choose Multiplatform Library from the Multiplatform > Library section:

  3. 输入名称说明,并为所有平台选择 "单个":Enter a Name and Description, and choose Single for all platforms:

  4. 完成向导。Complete the wizard. 在解决方案中创建单个库项目。A single library project is created in the solution.

  5. 右键单击新库项目,然后选择 "选项"。Right-click on the new library project and then select Options. "生成 > 常规" 部分允许设置目标框架–选择 ".net 可移植 PCL 配置文件" 或 ".NET Standard 版本":The Build > General section allows the Target Framework to be set – choose a .NET Portable PCL profile or a .NET Standard version:

  6. 同时,在 "项目选项" 窗口中,打开NuGet 包 "> 元数据" 部分,并输入所需的元数据(以及任何可选的元数据):Also in the Project Options window, open the NuGet Package > Metadata section and enter the required metadata (as well as any optional metadata):

  7. 右键单击库项目,然后选择 "创建 NuGet 包" (或 "生成或部署解决方案"),并将nupkg NuGet 包文件保存在 /Bin/ 文件夹中("调试" 或 "发布",具体取决于配置):Right-click on the library project and choose Create NuGet Package (or build or deploy the solution) and the .nupkg NuGet package file will be saved in the /bin/ folder (either Debug or Release, depending on configuration):

验证输出Verifying the Output

NuGet 包也是 ZIP 文件,因此可以检查生成的包的内部结构。NuGet packages are also ZIP files, so it's possible to inspect the internal structure of the generated package.

此屏幕截图显示了基于 PCL 的 NuGet 的内容–只包含一个 PCL 程序集:This screenshot shows the contents of a PCL-based NuGet – only a single PCL assembly is included:

添加特定于平台的代码Adding Platform-Specific Code

基于 PCL 的项目和基于 .NET Standard 的项目不能包含特定于平台的引用(例如 iOS 或 Android 功能)。PCL-based projects and .NET Standard-based projects cannot contain platform-specific references (such as iOS or Android functionality).

如果需要扩展现有 PCL 项目或 .NET Standard 项目以包含特定于平台的代码,则可以通过右键单击该项目并选择 "添加 > 添加平台实现 ... " 来完成此操作:If an existing PCL project or .NET Standard project needs to be expanded to include platform-specific code, this can be done by right-clicking on the project and selecting Add > Add Platform Implementation...:

可以将一个或多个平台项目添加到解决方案中,并且可以选择将现有 PCL 或 .NET Standard 库转换为共享项目:One or more platform projects can be added to the solution, and the existing PCL or .NET Standard library can optionally be converted to a Shared Project:

转换为共享项目后,请访问项目选项 > NuGet 包 > 引用程序集" 部分,并确保已选择任何所需的配置文件(以便 NuGet 继续与它所处的项目兼容以前在中使用)。After converting to a Shared Project, visit the Project Options > NuGet Package > Reference Assemblies section and ensure that any required profiles are selected (so that the NuGet continues to be compatible with projects it was previously used in).