Do I need a view controller for MVVM-light in Silverlight?

I have the following question regarding MVVM lighting: what controls the user interface? I see that I have a ViewModel per View that I'm showing; ViewLocator handles all ViewModels (for caching, as I understand it). But what drives the user interface?

If I have a command defined in my ViewModel that says "ShowDetail"; Do I need to write code to display this view inside the ViewModel?

Are there any examples of this? Thanks!

+6
mvvm-light
source share
5 answers

The MVVM template itself has nothing specific to navigate between views. Despite this, many platforms have many solutions. The most common solution is to use some kind of controller, which "organizes" the main view or uses the "Master-Detail" approach for prying.

Some interesting solutions:

+5
source share

In MVVM, that β€œdrives” of a view are data binding. You can connect the View to the ViewModel by setting the ViewConttext to point to the view model.

Simple example (using MVVM Light):

MyViewModel.cs

public class MyViewModel : ViewModelBase { (...) private string _myProperty; public string MyProperty { get { return _myProperty; } set { _myProperty = value; RaisePropertyChanged("MyProperty"); } } } 

Myview.xaml.cs

 void MyView() { DataContext = new MyViewModel(); } 

Myview.xaml

 <TextBlock Text="{Binding MyProperty}" /> 
+1
source share

I created a T4 template that generates code and shows how to go to uri or object, or close a window (wpf). It works with mvvm light

Download here

+1
source share

I think you could check out Cinch V2:

http://www.codeproject.com/KB/WPF/CinchV2_1.aspx

which seems quiet promising. However, I find that most of these structures are rather complicated.

I implemented a solution with a simple MVVM approach with some kind of controlling controller template that handles the relationship between Views and View-Models.

0
source share

I would advise you to familiarize yourself with the MVVM messaging system. This is perhaps the easiest approach I have found for this. Here is an example of how it works:

If you have 2 viewing models - 1 for searching customers, another for displaying information about the selected client:

In the first view model, you have a property like this:

 public string CustomerID { get { return _customerid; } set { if (_efolderid == value) { return; } var oldValue = _customerid; _customerid = value; // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging RaisePropertyChanged("CustomerID", oldValue, value, true); } } 

Then, in the second viewing model, you register to receive messages when this value changes from another, for example:

  void registerForMessages() { Messenger.Default.Register<PropertyChangedMessage<string>>(this, (pcm) => { if (pcm.PropertyName == "CustomerID") { customerID = pcm.NewValue; AddWorkplanCommand.RaiseCanExecuteChanged(); loadCustomerDetails(); } }); } 

Be sure to call the registerForMessages () function in the constructor of the second view model. Another thing that helps is to create a map when you have 4 or more ViewModels in your application. I find it easy to build it in a quick text file in the solution in order to keep track of all the messages and what they are designed to run, and what other view models are logged to receive them.

One of the very nice things about this is that you have 1 view model that sends a change notification, such as the customerID property, and 4 other view modes immediately appear, and they all start downloading the changes themselves.

0
source share

All Articles