A popular topic that comes up when talking about MVVM is the use of a ViewModelLocator and the many different ways one can be implemented. Rather than getting into the pros and cons on when or why you should use it, I decided I would just post my version of a simple ViewModelLocator and let those who like it use it, and those who don’t, well you know… :)
First, a disclaimer. I have not used this code in a production application, it is just something I was tossing around while reading others’ posts on the subject.
1. MainView.xaml
2. MainViewModel.cs
3. ViewModelLocator.cs
I have a codepaste of the ViewModelLocator.cs file if you are interested but don’t feel like re-typing the 50 lines of code!
Enjoy!
Additional Resources