在 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 中開啟專案時,在Solution Pad中以滑鼠右鍵按一下 [相依性] 資料夾(如果使用 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.

    新增 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 套件。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 套件可供使用時,就會以 circle](media/nuget-walkthrough-update-icon.png)![向上箭號來顯示更新圖示。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

從 [solution pad] 中,您可以查看目前已安裝的套件版本,並以滑鼠右鍵按一下要更新的套件。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. 當您選擇管理解決方案的套件時,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 則是使用 Microsoft. 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. 如果來源是安全網頁伺服器,也請輸入使用者名稱和密碼,否則請將這些項目留白: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