How to use MessageBox in MVVM?

The XAML in the MVVM pattern seems to make it harder to pop up messages. My client insists that check marks and colors are not suitable for them. They still need a mailbox. How can I do that?

I know that message pop-ups are in the view model, but that violates the whole purpose of the view model. I can also throw an error and pop up a message box in some exception handlers, but the message is no exception. This is part of the regular program flow.

Is there a good way to do this in XAML? My client loves messages. She does not care about the MVVM pattern, before using MVVM and unit test she never had quality problems. But now she cannot even receive her messages, so she is not very happy.

+7
unit-testing testing mvvm messagebox
source share
5 answers

One option is to use an interface for a mailbox such as

public interface IMessageBoxProvider { MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult); } 

and a wrapper class that implements this interface and uses a regular or custom mailbox. In viewmodel you can use this as

 private IMessageBoxProvider MessageBox { get; set; } 

where MessageBox is a wrapper class. So, now you have separated the actual message box and, thus, you can conduct a single test, and what not.

+2
source share

Josh Smith also contains a CodeProject article that may interest you here .

+2
source share

You may have a PopUpNotificationRequested event in your ViewModel that will be processed in the view to display message boxes. Thus, the message box display logic remains in view mode, but is still disconnected from the view.

+1
source share

The WPF Application Framework (WAF) ViewModel sample application shows how to display a MessageBox without breaking the MVVM pattern.

+1
source share

I ran into this problem a few weeks ago. I came across this article ( http://blog.roboblob.com/2010/01/19/modal-dialogs-with-mvvm-and-silverlight-4/ ) and essentially performed a very similar process for displaying modal dialogs with the MVVM template . To test my ViewModels, just create a fancy modal dialog service. Hope this helps too.

0
source share

All Articles