在 Mac 視覺化工作室中安裝和管理 NuGet 套裝軟體Install and manage NuGet packages in Visual Studio for Mac

適用于 Mac 的 Visual Studio 中的 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核心和 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.

有關在 Mac 視覺化工作室中使用 NuGet 的介紹,請參閱快速入門:在 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. 在 Mac 的 Visual Studio 中打開專案後,按右鍵解決方案墊中的 "依賴項"資料夾(如果使用 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 Pad 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.

    添加實體框架 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 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 核心專案.NET Core Projects

    • 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.

查找過期包Locating outdated packages

在解決方案墊中,您可以查看當前安裝的包版本,並按右鍵要更新的包。From the solution pad, 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. 當您選擇管理解決方案的包時,Mac 的 Visual Studio 通過在"打包管理器"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.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檢索可用於安裝的包。但是,您可以將其他包位置添加到 Mac 的 Visual Studio。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. 如果來源是安全網頁伺服器,也請輸入使用者名稱和密碼,否則請將這些項目留白:If the source is a secure web server, enter the username and password as well, otherwise leave these entries blank:

新增套件來源

搜尋套件時,可以選取不同的來源:Different sources can then be selected when searching for packages:

新增套件來源

版本控制Version Control

NuGet 文件討論 using NuGet without committing packages to source control (在未將套件認可到原始檔控制的情況下使用 NuGet)。The 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