What is NuGet?
NuGet is a package manager that allows you to add packages (such as assemblies and source code) to an existing project. All versions of MVVM Light are supported through NuGet. To learn more and install NuGet, visit nuget.org.
Finding and Installing MVVM Light with NuGet
To add MVVM Light to your project using NuGet, follow these steps:
- Open your project in Visual Studio.
- Right-click on the References folder and select Manage NuGet Packages… from the context menu.Note: If this option is missing, you need to install the NuGet package manager from nuget.org.
- In the Manage NuGet Packages dialog, select Online on the right.
- Enter “mvvmlight” in the search field.
- Select the MVVM Light package and click the Install button (choose the top option for the full package).
Changes to Your Project
After installation, the following changes will be made to your project:
- A packages folder will be added containing the necessary libraries. This folder is added to the root of the project structure but does not appear in the Solution Explorer.
- A ViewModel folder will be added with two classes:
ViewModelLocator.cs
MainViewModel.cs
- The
App.xaml
file will be modified to include theViewModelLocator
as a global resource.
Binding the DataContext in XAML
With the new file structure, the ViewModelLocator
is exposed in App.xaml
(global resources) and can be used as the source of the DataContext bindings. The MainViewModel
added to the project is exposed in the ViewModelLocator
as a property. For more details on this architecture, refer to Understanding the MVVM Pattern and Deep Dive MVVM.
Note: If you don’t need MainViewModel
in your project, you can remove this class.
To use MainViewModel
as the DataContext of the MainPage
or MainWindow
, follow these steps:
- Open
MainPage.xaml
orMainWindow.xaml
. - Add the following line to the opening tag of
MainPage
/MainWindow
:xml
Binding the DataContext in Blend
You can also bind the DataContext visually in Blend:
- Open your project in Blend.
- Build the application.
- Open
MainPage
/MainWindow
. - In the Objects and Timeline panel, select the top element (Window, Page, etc.).
- In the Properties panel, select the DataContext property (in the Common Properties section).
- Click the small advanced options peg next to the property field and select Data Binding from the context menu.
- In the Data Binding editor, select the Data Field tab and the
Locator
data source. Note: If you don’t see theLocator
field, you need to build the project. - In the Fields area, expand
ViewModelLocator
and select theMain
property. Click OK to close the Data Binding editor.
Installing Without the ViewModel Folder (Libraries Only)
If you’re adding MVVM Light to an existing application with an existing ViewModelLocator
and/or viewmodels, you should install the “libraries only” NuGet package. Follow the steps above, but select MVVM Light libraries only instead of the full MVVM Light package.
Installing Portable Class Libraries
To install the portable class libraries (PCL) version of MVVM Light, follow the steps above and select MVVM Light libraries only (PCL).