The technique below is one technique, others exist, but the design goals of this approach are:
- The ViewModel cannot know or reference the View
- The ViewModel should should not care how the View displays or interacts with the user (it could be a dialog box, a flyout, etc.)
- The ViewModel must be Portable Class Library compatible and View agnostic, e.g. not need to know about dialog buttons, etc.
- Choices the user makes in the View dialog should result in Commands being executed on the ViewModel
- The ViewModel must not specify the content of the message text, button labels, etc. – the View should be responsible for the text/content of the message
One way to think about these design goals is that the ViewModel is asking for some semantic message/dialog to be displayed in the View.
Defining a Message to be used with the MVVM Light Messenger
The way the ViewModel “tells” the View to create a dialog is by sending a message using the MVVM Light Messenger.
First we define a custom message:
using System.Windows.Input;
using GalaSoft.MvvmLight.Messaging;
namespace Project.Portable.ViewModel.Messages
{
public class ShowDialogMessage : MessageBase
{
public ICommand Yes { get; set; }
public ICommand No { get; set; }
public ICommand Cancel { get; set; }
}
}
This message allows us to define the commands that the view will call, based on the choice that the user makes in the dialog.
More...
SHARE:
This article is part of the 50 Apps by Christmas series.
I developed a Silverlight app a few years ago and I wanted to see how easy it would be to port to a Windows Store app. You can download free trial of SilverKeys for Windows 8.
The Original Silverlight App
Here’s a screenshot of the original Silverlight app:
The app helps amateur composers try out different scales and also helps them understand what common chord progressions are often used.
More...
SHARE: