1

I've to create a small windows application(no silverlight, no windows phone, no windows 8 app, ...), and since I've some extra for this application, I decided to take the time to learn the MVVM pattern.

I read about it and I understood the principal part.

I found the MVVM Light Toolkit, but I find hard to start with. Except this page where I find the class names, I can't find any documentation about this, some kind of "getting started" section.

I tried to start with that, but I can't find:

  • What is the ViewModelLocator, and how should I use it
  • A simple example of a ViewModel, extending the ViewModelBase (for example, I don't know if I should call RaisePropertyChanging, RaisePropertyChanged, both, if I should put just the property name, ...
  • Some example about how to use the Messenger (in which part of the code I should register, examples of conditions)

After some search on the net, either I find informations for windows phone(with Page, ...), either it's years old topic.

So do you know where I can find this kind of information? Thank you!

STiLeTT
  • 1,023
  • 10
  • 23
J4N
  • 19,480
  • 39
  • 187
  • 340
  • I would suggest this book for you: http://www.amazon.com/Pro-WPF-Silverlight-MVVM-Model-View-ViewModel/dp/1430231629 and the whole mvvm stuff will be more easy for you – Peter Porfy Nov 26 '12 at 13:23
  • @PeterPorfy It doesn't seems to be very well rated 2.4/5. In addition, it doesn't talk about MVVM light toolkit :( – J4N Nov 26 '12 at 13:33
  • You may be interested in this question for your first point: [What is a ViewModelLocator and what are its pros/cons compared to DataTemplates?](http://stackoverflow.com/q/5462040/302677) – Rachel Nov 26 '12 at 14:19
  • @J4N I read it and it's great. I don't know the reason of the rating. MVVM Light is just a collection of basic MVVM helpers and base classes, nothing special. – Peter Porfy Nov 26 '12 at 14:44
  • @PeterPorfy I will try to read some page before, because I'm a little worried about this book contains a lot of things that I already know(all the WPF-binding-serialization-events-command-data access layer-unit testing part). What I miss is just how this framework put those pieces together, part that this book doesn't cover. – J4N Nov 26 '12 at 15:01

1 Answers1

1

To answer your questions:

The ViewModelLocator is a class that allows you to expose your various models via properties, and do whatever initialization is required. You can then bind the DataContext of a page or control conveniently to a ViewModel.

DataContext="{Binding Main, Source={StaticResource Locator}}"

Here is a similar question

Call RaisePropertyChanged([name-of-property-here]) to update your bindings.

Use the Messenger as a way to easily notify your page when the state of the application has changed, to display an error message when an error has occurred is a good example. To use Messenger, you can register in OnNavigatedTo

Messenger.Default.Register<YourCustomMessage>(this, OnYourCustomMessage);

and don't forget to unregister in OnNavigatedFrom

Messenger.Default.Unregister<YourCustomMessage>(this, OnYourCustomMessage);
Community
  • 1
  • 1
kindasimple
  • 2,427
  • 1
  • 16
  • 20
  • Thank you for your response! Do you have a full example for the Locator? Because I can't see, by example if I've a list of people and a person detail view, how it will know which "person" of my datastore the viewModel has to be based on? For the messenger, I'm not sure to fully understand, it allows the application logic(e.g. server connection watchdog) to interact with view, or it allows view to interact to application logic? On which object are your OnNavigatedTo/OnNavigatedFrom methods ? – J4N Nov 26 '12 at 13:29
  • Heres an app someone posted as an example. Its WPF, but essentialy the same. It uses Unity for DI, but you may want to just manually inject your dependencies when you new up a class. http://apuntanotas.codeplex.com/SourceControl/changeset/view/79962 – kindasimple Nov 26 '12 at 15:44
  • I recommend sending messages from the view models and receive them in the views or other view models. Its easier than chaining event handlers to respond to an event. You send a message like this: Messenger.Default.Send(new YourCustomMessage()); To send a message from a view to a viewmodel I usually execute RelayCommands – kindasimple Nov 26 '12 at 15:50
  • So OnNavigatedTo/... are DI methods? I only used ninject for DI, I will take a look into it! Thank you for all your explanation! – J4N Nov 26 '12 at 16:24
  • No, OnNavigatedTo is a virtual member of your page. You can override it in your view. http://msdn.microsoft.com/en-us/library/windowsphone/develop/ms611620(v=vs.105).aspx – kindasimple Nov 26 '12 at 17:34