5/10/2018 Introductory Guide to Using NuGet Packages from within Visual Studio | Microsoft Docs
NuGet packages contain reusable code that other developers make available to you for use in your projects. See What is NuGet? for background.
Packages are installed into a Visual Studio project using the Package Manager UI or the Package Manager Console. This article demonstrates the
process using the popular [Link] package and a Universal Windows Platform (UWP) project. The same process applies to any other .NET or
.NET Core project.
Once installed, refer to the package in code with using <namespace> where <namespace> is specific to the package you're using. Once the reference is
made, you can call the package through its API.
Tip
Start with [Link]: Browsing [Link] is how .NET developers typically find components they can reuse in their own applications. You can
search [Link] directly or find and install packages within Visual Studio as shown in this article.
Prerequisites
Visual Studio 2017 with the Universal Windows Platform development workload, or
Visual Studio 2015 Update 3 with Tools for Universal Windows Apps.
You can install the 2017 Community edition for free from [Link] or use the Professional or Enterprise editions.
Create a project
NuGet packages can be installed into any .NET project, provided that the package supports the same target framework as the project.
For this walkthrough, use a simple Universal Windows (UWP) app. Create a project in Visual Studio using File > New Project... and selecting the
Windows Universal > Blank App (Universal Windows). Accept the default values for Target Version and Minimum Version when prompted.
Add the [Link] NuGet package
[Link] 1/8
5/10/2018 Introductory Guide to Using NuGet Packages from within Visual Studio | Microsoft Docs
To install the package, you can use either the Package Manager UI or the Package Manager Console. When you install a package, NuGet records the
dependency in either your project file or a [Link] file. For more information, see Package consumption overview and workflow.
Package Manager UI
1. In Solution Explorer, right-click References and choose Manage NuGet Packages.
[Link] 2/8
5/10/2018 Introductory Guide to Using NuGet Packages from within Visual Studio | Microsoft Docs
2. Choose "[Link]" as the Package source, select the Browse tab, search for [Link], select that package in the list, and select Install:
3. Accept any license prompts.
4. (Visual Studio 2017) If prompted to select a package management format, select PackageReference in project file:
[Link] 3/8
5/10/2018 Introductory Guide to Using NuGet Packages from within Visual Studio | Microsoft Docs
5. If prompted to review changes, select OK.
Package Manager Console
1. Select the Tools > NuGet Package Manager > Package Manager Console menu command.
2. Once the console opens, check that the Default project drop-down list shows the project into which you want to install the package. If you have a
single project in the solution, it is already selected.
[Link] 4/8
5/10/2018 Introductory Guide to Using NuGet Packages from within Visual Studio | Microsoft Docs
3. Enter the command Install-Package [Link] (see Install-Package). The console window shows output for the command. Errors typically
indicate that the package isn't compatible with the project's target framework.
Use the [Link] API in the app
With the [Link] package in the project, you can call its [Link] method to convert an object to a human-readable
string.
1. Open [Link] and replace the existing Grid element with the following:
XAML = Copy
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel VerticalAlignment="Center">
<Button Click="Button_Click" Content="Click Me" Margin="10"/>
<TextBlock Name="TextBlock" Text="TextBlock" Margin="10"/>
</StackPanel>
</Grid>
2. Open the [Link] file (located in Solution Explorer under the [Link] node), and insert the following code inside the MainPage
constructor:
C# = Copy
public class Account
{
public string Name { get; set; }
public string Email { get; set; }
public DateTime DOB { get; set; }
}
[Link] 5/8
5/10/2018 Introductory Guide to Using NuGet Packages from within Visual Studio | Microsoft Docs
private void Button_Click(object sender, RoutedEventArgs e)
{
Account account = new Account
{
Name = "John Doe",
Email = "john@[Link]",
DOB = new DateTime(1980, 2, 20, 0, 0, 0, [Link]),
};
string json = [Link](account, [Link]);
[Link] = json;
}
3. Even though you added the [Link] package to the project, red squiggles appears under JsonConvert because you need a using
statement at the top of the code file:
C# = Copy
using [Link];
4. Build and run the app by pressing F5 or selecting Debug > Start Debugging:
[Link] 6/8
5/10/2018 Introductory Guide to Using NuGet Packages from within Visual Studio | Microsoft Docs
5. Select on the button to see the contents of the TextBlock replaced with some JSON text:
[Link] 7/8
5/10/2018 Introductory Guide to Using NuGet Packages from within Visual Studio | Microsoft Docs
Related articles
Overview and workflow of package consumption
Finding and choosing packages
Ways to install a package
Configuring NuGet Behavior
[Link] 8/8