bobby's blog

randomly specific...


News

My Stats

  • Posts - 15
  • Comments - 48
  • Trackbacks - 0

Twitter












Recent Comments


Recent Posts


Archives


Post Categories


Image Galleries


My Links


User Groups



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

image

 

2. MainViewModel.cs

image


3. ViewModelLocator.cs

image

 

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

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Comments

Gravatar # re: KISS and Tell - MVVM and the ViewModelLocator
Posted by Ralph Shillington on 12/26/2010 4:13 PM
This is an interesting approach, the only downside being that the view is not discovered by waking the stack trace when in design time. It's been a while since you posted this snippet. Have you taken the ViewModelLocator dilemma any further..
Gravatar # re: KISS and Tell - MVVM and the ViewModelLocator
Posted by Investment Options on 6/3/2011 4:33 AM
This is the article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks soo much for this nice article.

Gravatar # re: KISS and Tell - MVVM and the ViewModelLocator
Posted by Radek on 6/16/2011 3:14 PM
Hello Bobby,
i was interested in your conventional based viewmodellocator. But in production code i think that you cant use stacktrace as you did. Methods like Initialize component are not in stack trace. They are inlined because of optimization and whole stacktrace is completely different (there are no names of some methods etc.) from one when app is run with VS. But thx for your post it was great inspiration for my own implementation.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: