在 Visual Studio for Mac 中安装和管理 NuGet 包Install and manage NuGet packages in Visual Studio for Mac

通过 Visual Studio for Mac 中的 NuGet 包管理器 UI,可轻松安装、卸载和更新项目和解决方案中的 NuGet 包。The NuGet Package Manager UI in Visual Studio for Mac allows you to easily install, uninstall, and update NuGet packages in projects and solutions. 可以搜索包和将包添加到 .NET Core、ASP.NET Core 和 Xamarin 项目。You can search for and add packages to your .NET Core, ASP.NET Core, and Xamarin projects.

本文介绍如何在项目中包括 NuGet 包并演示实现此流程无缝的工具链。This article describes how to include a NuGet package in a project and demonstrates the tool chain that makes the process seamless.

有关在 Visual Studio for Mac 中使用 NuGet 的简介,请参阅快速入门:在 Visual Studio for Mac 中安装和使用包For an intro to using NuGet in Visual Studio for Mac, see Quickstart: Install and use a package in Visual Studio for Mac

查找和安装包Find and Install a Package

  1. 对于 Visual Studio for Mac 中打开的项目,右键单击“解决方案窗口”中的“依赖项”文件夹(如果使用 Xamarin 项目,则为“包”文件夹)并选择“管理 NuGet 包...” 。With a project open in Visual Studio for Mac, right-click on the Dependencies folder (Packages folder if using a Xamarin project) in the Solution Window and select Manage NuGet Packages....

    添加新的 NuGet 包上下文操作

  2. 此时会启动“管理 NuGet 包”窗口。This launches the Manage NuGet Packages window. 确保对话框左上角的“源”下拉列表已设置为 nuget.org,以便可搜索中央 NuGet 包存储库。Ensure that the Source drop-down in the top left corner of the dialog is set to nuget.org, so that you're searching the central NuGet package repository.

    列出 NuGet 包

  3. 使用右上角的搜索框查找特定的包,如 EntityFrameworkUse the search box in the top-right corner to find a specific package, for example EntityFramework. 找到希望使用的包后,请选择它并单击“添加包”按钮以开始安装。When you have found a package that you wish to use, select it and click the Add Package button to begin installation.

    添加 EntityFramework NuGet 包

  4. 包下载完毕后会添加到项目中。Once the package has been downloaded, it will be added to your project. 解决方案将根据正在编辑的项目类型而发生变化:The solution will change depending on the type of project you're editing:

    Xamarin 项目Xamarin Projects

    • “引用”节点包含属于 NuGet 包的所有程序集列表。The References node will contain a list of all the assemblies that are part of a NuGet package.
    • “包”节点显示每个已下载的 NuGet 包。The Packages node displays each NuGet package that you have downloaded. 可以更新该列表中的包,或从列表中删除包。You can update or remove a package from this list.

    .NET Core 项目.NET Core Projects

    • “NuGet”节点(“依赖项”>“NuGet”)显示每个已下载的 NuGet 包。The Dependencies > NuGet node displays each NuGet package that you have downloaded. 可以更新该列表中的包,或从列表中删除包。You can update or remove a package from this list.

使用 NuGet 包Using NuGet Packages

添加 NuGet 包并更新项目引用后,可以如针对任何项目引用一样根据 API 对其进行编程。Once the NuGet package has been added and the project references updated, you can program against the APIs as you would with any project reference.

请确保将任何所需的 using 指令添加到文件顶部:Ensure that you add any required using directives to the top of your file:

using Newtonsoft.Json;

更新包Updating Packages

通过右键单击“依赖项”节点(对于 Xamarin 项目为“包”节点)可以一次性完成所有包更新,也可以在每个包上单独进行包更新。Package updates can be done either all at once, by right-clicking on the Dependencies node (Packages node for Xamarin projects), or individually on each package. 提供新版本的 NuGet 包时,更新图标会显示为带有圆圈的向上箭头When a new version of a NuGet package is available, an update icon appears Up arrow with circle.

右键单击“依赖项”,以访问上下文菜单,然后选择“更新”以更新所有包:Right-click on Dependencies to access the context menu and choose Update to update all packages:

突出显示“更新”菜单的“依赖项”上下文菜单

  • 管理 NuGet 包 - 打开窗口,将更多包添加到项目。Manage NuGet Packages - Opens the window to add more packages to the project.
  • 更新 - 检查每个包的源服务器并下载任何更新版本。Update - Checks the source server for each package and downloads any newer versions.
  • 还原 - 下载任何缺少的包(无需将现有包升级到更新版本)。Restore - Downloads any missing packages (without updating existing packages to newer versions).

现在也提供解决方案级别的“更新”和“还原”选项,这些选项可影响该解决方案中的所有项目。Update and Restore options are also available at the Solution level, and affect all the projects in the solution.

更新为包的预发行版本Updating to pre-release versions of packages

若要更新为包的较新预发行版本,可以右键单击“依赖项”以打开上下文菜单,然后选择“管理 NuGet 包...”菜单 。To update to a newer pre-release version of a package, you can right-click on Dependencies to open the context menu and choose the Manage NuGet Packages... menu.

突出显示“管理 NuGet 包...”菜单的“依赖项”上下文菜单

选中对话框底部的“显示预发行包”复选框。Check the Show pre-release packages checkbox at the bottom of the dialog.

打开的“管理 NuGet 包”对话框,其中选中了“显示预发行包”选项

最后,从对话框的“更新”选项卡中,选择要更新的包,然后从“新版本”下拉列表中选择新的预发行版本并单击“更新包” 。Finally, from the Updates tab of the dialog, select the package you wish to update and choose the new pre-release version from the New Version drop-down and click Update Package.

打开到“已安装”选项卡的“管理 NuGet 包”对话框,其中选择了一个包并打开了“新版本”下拉列表。

查找过时的包Locating outdated packages

在解决方案窗口中,可以查看当前已安装包的版本,可右键单击包进行更新。From the Solution Window, you can view what version of a package is currently installed and right-click on the package to update.

包含“更新”、“删除”和“刷新”选项的包菜单

包存在可用新版本时,包名称旁会显示通知,你可以决定是否更新包。You will also see a notification next to the package name when a new version of a package is available, so you can decide if you may want to update it.

包存在可用新版本时显示的通知

显示的菜单中包含两个选项:In the menu shown, you have two options:

  • 更新 - 检查源服务器并下载更新版本(如果存在)。Update - Checks the source server and downloads a newer version (if it exists).
  • 删除 - 从此项目中删除包,并从项目引用中删除相关的程序集。Remove - Removes the package from this project and removes the relevant assemblies from the project's References.

管理解决方案的包Manage packages for the solution

管理解决方案的包是同时处理多个项目的便捷方式。Managing packages for a solution is a convenient means to work with multiple projects simultaneously.

  1. 右键单击解决方案,并选择“管理 NuGet 包…”:Right-click the solution and select Manage NuGet Packages...:

    管理解决方案的 NuGet 包

  2. 管理解决方案的包时,UI 让你可以选择受操作影响的项目:When managing packages for the solution, the UI lets you select the projects that are affected by the operations:

    管理解决方案的包时的项目选择器

“合并”选项卡Consolidate tab

在具有多个项目的解决方案中操作时,最佳做法是确保无论在每个项目的何处使用同一 NuGet 包,都要使用该包的同一版本号。When working in a solution with multiple projects, it's considered a best practice to make sure that anywhere you use the same NuGet package in each project, you're also using the same version number of that package. 在选择管理某解决方案的包时,Visual Studio for Mac 会在包管理器 UI 中提供“合并”选项卡,为你简化这一操作。Visual Studio for Mac helps make this easier by providing a Consolidate tab in the Package Manager UI when you choose to manage packages for a solution. 借助此选项卡,你可轻松查看解决方案中的不同项目在哪些位置使用具有不同版本号的包:Using this tab, you can easily see where packages with distinct version numbers are used by different projects in the solution:

包管理器 UI“合并”选项卡

在本例中,NuGetDemo 项目使用的是 Microsoft.EntityFrameworkCore 2.20,而 NuGetDemo.Shared 使用的是 Microsoft.EntityFrameworkCore 2.2.6。In this example, the NuGetDemo project is using Microsoft.EntityFrameworkCore 2.20, whereas NuGetDemo.Shared is using Microsoft.EntityFrameworkCore 2.2.6. 若要合并包版本,请执行以下操作:To consolidate package versions, do the following:

  • 在项目列表中选择要更新的项目。Select the projects to update in the project list.
  • 在“新版本”列表中选择要在上述所有项目中使用的版本,例如 Microsoft.EntityFrameworkCore 3.0.0。Select the version to use in all those projects in the New Version list, such as Microsoft.EntityFrameworkCore 3.0.0.
  • 选择“合并包”按钮。Select the Consolidate Package button.

包管理器将选定的包版本安装到所有选定的项目中,之后包不再显示在“合并”选项卡上。The Package Manager installs the selected package version into all selected projects, after which the package no longer appears on the Consolidate tab.

添加包源Adding Package Sources

最初从 nuget.org 检索安装包。然而,可以将其他包位置添加到 Visual Studio for Mac。Packages available for installation are initially retrieved from nuget.org. However, you can add other package locations to Visual Studio for Mac. 这有助于测试自己正在开发的 NuGet 包,或有助于使用公司或组织内的专用 NuGet 服务器。This can be useful for testing your own NuGet packages under development, or to use a private NuGet server inside your company or organization.

在 Visual Studio for Mac 中,导航到“Visual Studio”>“首选项”>“NuGet”>“源”,查看和编辑包源的列表。In Visual Studio for Mac, navigate to Visual Studio > Preferences > NuGet > Sources to view and edit the list of package sources. 请注意,源可以是(由 URL 指定的)远程服务器或本地目录。Note that sources can be a remote server (specified by a URL) or a local directory.

包源

单击“添加”设置新源。Click Add to set-up a new source. 向包源输入友好名称和 URL(或文件路径)。Enter a friendly name and the URL (or file path) to the package source. 如果源是安全的 Web 服务器,则同时输入用户名和密码,否则请将这些条目留空:If the source is a secure web server, enter the username and password as well, otherwise leave these entries blank:

“添加包源”对话框,其中提示输入名称、位置 URL、用户名和密码。

搜索包时可以选择不同的源:Different sources can then be selected when searching for packages:

“添加包源”对话框,显示包含包源列表的下拉列表。

版本控制Version Control

NuGet 文档讨论了在不将包提交到源控件的情况下使用 NuGetThe NuGet documentation discusses using NuGet without committing packages to source control. 如果不希望将二进制文件和未使用信息存储到源代码管理中,可以将 Visual Studio for Mac 配置为自动从服务器还原包。If you prefer not to store binaries and unused information in source control, you can configure Visual Studio for Mac to automatically restore packages from the server. 这意味着当开发人员首次从源代码管理检索项目时,Visual Studio for Mac 将自动下载安装所需的包。This means that when a developer retrieves the project from source control for the first time, Visual Studio for Mac will automatically download and install the required packages.

自动还原包

有关如何使 packages 目录不受跟踪的详细信息,请参阅特定的源控件文档。Refer to your particular source control documentation for details on how to exclude the packages directory from being tracked.

请参阅See also