快速入門:在 Visual Studio for Mac 中安裝和使用套件Quickstart: Install and use a package in Visual Studio for Mac

NuGet 套件包含可重複使用的程式碼,由其他開發人員提供您在專案中使用。NuGet packages contain reusable code that other developers make available to you for use in your projects. 請參閱什麼是 NuGet?了解背景知識。See What is NuGet? for background. 封裝會使用 NuGet 套件管理員安裝到 Visual Studio for Mac 專案中。Packages are installed into a Visual Studio for Mac project using the NuGet Package Manager. 本文示範如何使用熱門的Newtonsoft套件和 .net Core 主控台專案來處理常式。This article demonstrates the process using the popular Newtonsoft.Json package and a .NET Core console project. 相同的程式也適用于其他任何 Xamarin 或 .NET Core 專案。The same process applies to any other Xamarin or .NET Core project.

安裝之後,請使用 using <namespace> 參考程式碼中的套件,其中 <namespace> 為您使用的套件專用。Once installed, refer to the package in code with using <namespace> where <namespace> is specific to the package you're using. 建立參考之後,您可以透過其 API 呼叫套件。Once the reference is made, you can call the package through its API.

提示

從 nuget.org 開始:.NET 開發人員通常是透過瀏覽 nuget.org 來尋找可在自己應用程式中重複使用的元件。Start with nuget.org: Browsing nuget.org is how .NET developers typically find components they can reuse in their own applications. 您可以直接搜尋 nuget.org,或在 Visual Studio 中尋找並安裝套件,如此文章所示。You can search nuget.org directly or find and install packages within Visual Studio as shown in this article. 如需一般資訊,請參閱尋找和評估 NuGet 套件For general information, see Find and evaluate NuGet packages.

必要條件Prerequisites

  • 適用于 Mac 的 Visual Studio 2019。Visual Studio 2019 for Mac.

您可以從 visualstudio.com 免費安裝 2019 Community Edition,或使用 Professional Edition 或 Enterprise Edition。You can install the 2019 Community edition for free from visualstudio.com or use the Professional or Enterprise editions.

如果您是在 Windows 上使用 Visual Studio,請參閱在 Visual Studio 中安裝和使用套件(僅限 Windows)If you're using Visual Studio on Windows, see Install and use a package in Visual Studio (Windows Only).

建立專案Create a project

假設 NuGet 套件 支援與專案相同的目標架構,則該套件就可安裝到任何的 .NET 專案中。NuGet packages can be installed into any .NET project, provided that the package supports the same target framework as the project.

在此逐步解說中,請使用簡單的 .NET Core 主控台應用程式。For this walkthrough, use a simple .NET Core Console app. 在 Visual Studio for Mac 中使用 [檔案 > 新方案] 建立專案,然後選取 [ .Net Core > 應用程式] > 主控台應用程式範本。Create a project in Visual Studio for Mac using File > New Solution..., select the .NET Core > App > Console Application template. 按一下 [下一步]。Click Next. 出現提示時,接受目標 Framework的預設值。Accept the default values for Target Framework when prompted.

Visual Studio 隨即建立專案,而專案會在 [方案總管] 中開啟。Visual Studio creates the project, which opens in Solution Explorer.

新增 Newtonsoft.Json NuGet 套件Add the Newtonsoft.Json NuGet package

若要安裝套件,您可以使用 NuGet 套件管理員。To install the package, you use the NuGet Package Manager. 當您安裝套件時,NuGet 會將相依性記錄在您的專案檔packages.config或檔案中(視專案格式而定)。When you install a package, NuGet records the dependency in either your project file or a packages.config file (depending on the project format). 如需詳細資訊,請參閱套件使用概觀和工作流程For more information, see Package consumption overview and workflow.

NuGet 封裝管理員NuGet Package Manager

  1. 在方案總管中,以滑鼠右鍵按一下 [相依性],然後選擇 [新增套件 ... ]In Solution Explorer, right-click Dependencies and choose Add Packages....

    專案參考的管理 NuGet 套件命令

  2. 選擇 [nuget.org] 作為對話方塊左上角的 [套件來源],然後搜尋 [ Newtonsoft],在清單中選取該套件,然後選取 [新增套件 ... ]:Choose "nuget.org" as the Package source in the top left corner of the dialog, and search for Newtonsoft.Json, select that package in the list, and select Add Packages...:

    尋找 Newtonsoft.Json 套件

    如果您需要 NuGet 套件管理員的詳細資訊,請參閱使用 Visual Studio for Mac 安裝和管理套件If you want more information on the NuGet Package Manager, see Install and manage packages using Visual Studio for Mac.

在應用程式中使用 Newtonsoft.Json APIUse the Newtonsoft.Json API in the app

在專案中使用 Newtonsoft.Json 套件,您可以呼叫其 JsonConvert.SerializeObject 方法,將物件轉換成人類可閱讀的字串。With the Newtonsoft.Json package in the project, you can call its JsonConvert.SerializeObject method to convert an object to a human-readable string.

  1. Program.cs開啟檔案(位於 Solution Pad),並將檔案內容取代為下列程式碼:Open the Program.cs file (located in the Solution Pad) and replace the file contents with the following code:

    using System;
    using Newtonsoft.Json;
    
    namespace NuGetDemo
    {
        public class Account
        {
            public string Name { get; set; }
            public string Email { get; set; }
            public DateTime DOB { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Account account = new Account()
                {
                    Name = "Joe Doe",
                    Email = "joe@test.com",
                    DOB = new DateTime(1976, 3, 24)
                };
                string json = JsonConvert.SerializeObject(account);
                Console.WriteLine(json);
            }
        }
    }
    
  2. 藉由選取 [執行] > 開始調試程式來建立並執行應用程式:Build and run the app by selecting Run > Start Debugging:

  3. 應用程式執行後,您會看到序列化的 JSON 輸出出現在主控台中:Once the app runs, you'll see the serialized JSON output appear in the console:

主控台應用程式的輸出

後續步驟Next steps

恭喜,您安裝並使用了您的第一個 NuGet 套件!Congratulations on installing and using your first NuGet package!

若要深入探索 NuGet 所提供的功能,請選取下列連結。To explore more that NuGet has to offer, select the links below.