1,什么是Nuget
對于.NET(包括.NET Core),Microsoft支持的代碼共享機(jī)制是NuGet,它定義了.NET程序包的創(chuàng)建,托管和使用方式,并為每個角色提供了工具。
2,安裝nuget
https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
https://docs.microsoft.com/en-us/nuget/install-nuget-client-tools
3,在dotnet CLI(命令行)中安裝和使用軟件包,NuGet軟件包可以安裝到某種.NET項目中。創(chuàng)建一個簡單的.NET Core控制臺項目
1,選擇路徑,創(chuàng)建一個文件夾,打開命令行并切換到這個文件夾,
2,使用以下命令創(chuàng)建項目:dotnet new console

3.使用dotnet run測試該應(yīng)用程序已被正確創(chuàng)建。

4,添加Newtonsoft.Json NuGet包

dotnet add package <PACKAGE_NAME> 安裝軟件包
dotnet add package <PACKAGE_NAME> -v <VERSION>? 安裝特定版本的軟件包
dotnet list package? 列出項目的軟件包引用
dotnet remove package <PACKAGE_NAME>? 從項目文件中刪除軟件包引用。
dotnet restore?恢復(fù)項目文件中列出的軟件包
打開.csproj文件以查看添加的參考
4,在Visual Studio中安裝和使用程序包
首先,先創(chuàng)建一個項目,打開visual studio,選擇新建,項目,選擇WPF App (.NET Core)模板,創(chuàng)建一個新的項目

點(diǎn)擊工具,Nuget包管理器,程序包管理器控制臺,在程序包源中選擇nuget.org

點(diǎn)擊工具,Nuget包管理器,管理解決方案的nuget程序包,點(diǎn)擊瀏覽,搜索Newtonsoft.Json,在列表中選擇該軟件包,選擇項目,然后選擇安裝:

在軟件包管理器控制臺輸入命令Install-Package Newtonsoft.Json,這樣就在app中下載完成l
在app中使用包:
打開MainWindow.xaml并將現(xiàn)有Grid元素替換為以下內(nèi)容:
<Grid Background="White"> <StackPanel VerticalAlignment="Center"> <Button Click="Button_Click" Width="100px" HorizontalAlignment="Center" Content="Click Me" Margin="10"/> <TextBlock Name="TextBlock" HorizontalAlignment="Center" Text="TextBlock" Margin="10"/> </StackPanel> </Grid>
打開MainWindow.xaml.cs文件(位于MainWindow.xaml節(jié)點(diǎn)下的解決方案資源管理器中),然后在MainWindow類中插入以下代碼:
public class Account{ public string Name { get; set; } public string Email { get; set; } public DateTime DOB { get; set; }}private void Button_Click(object sender, RoutedEventArgs e){ Account account = new Account { Name = "John Doe", Email = "john@microsoft.com", DOB = new DateTime(1980, 2, 20, 0, 0, 0, DateTimeKind.Utc), }; string json = JsonConvert.SerializeObject(account, Formatting.Indented); TextBlock.Text = json;}
sonConvert由于using在代碼文件的頂部都需要一個語句
using Newtonsoft.Json;
在按鈕上選擇以查看TextBlock的內(nèi)容替換為一些JSON文本:(點(diǎn)擊調(diào)試,運(yùn)行)