michielpost.nl

MVVM Light: Cannot create an instance of "ViewModelLocator" on Windows 8

09-07-2012

When you create a new Windows 8 Metro style application and install MVVM Light using NuGet (full install), it will create a ViewModelLocator class in the ViewModel folder.

After you've modified your MainPage's datacontext to the new MainViewModel using this line:
DataContext="{Binding Source={StaticResource Locator}, Path=Main}"

Loading your view in the designer returns this error:
Cannot create an instance of "ViewModelLocator"

It looks like the included file for the ViewModelLocator in the NuGet package contains an error.

Quick fix:
Make sure your ViewModelLocator constructor is static:
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
static ViewModelLocator()
{

}

Comments


No comments yet.

New Comment

Name
E-mail (not published)
Comment
Enter the code shown: