views:

191

answers:

6

It seems that the XAML in MVVM pattern has difficulty to pop-up a Messageboxes. My client insists that the validation labels and colors are not good for them. They still want a messagebox. How can do it?

I know I can pop-up messageboxes in the view-model, but it violates the whole purpose for the view-model. I can also raise a error, and pop-up a messagebox in some exception handlers, but the messagebox is not an exception. It is part of the normal program flow.

Is there a good way to do it in XAML? My client likes messageboxes. She does not care about the MVVM pattern, she never had any quality problem before using MVVM and unit test. But now, she can not even get her messageboxes, so she is not very happy.

Your help is definitely appreciated ... I need to make her happy.

+1  A: 

You can have a PopUpNotificationRequested event in your ViewModel which will be handled by the View to show message boxes. This way the logic to show the messagebox stays in the viewmodel, but is still decoupled from the view.

Sijin
A: 

Thank you very much. But I did a google search for "PopUpNotificationRequested" and I could not get anything. Another thing: If I use "PopUpNotificationRequested", I am still touching the UI directly, does this violate the MVVM pattern?

BigTiger
+1  A: 

One possibilty is to use an interface for the messagebox like

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 normal or custom messagebox. In the viewmodel you can then use like this

private IMessageBoxProvider MessageBox { get; set; }

where MessageBox is the wrapper class. So now you have decoupled the actual messagebox and so you are able to to unit testing and what not.

AKrebs
A: 

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 followed a very similar process for showing modal dialogs with the MVVM pattern. For testing my ViewModels simply just create a mock modal dialog service. I hope this helps you too.

dParker
+1  A: 

The ViewModel sample application of the WPF Application Framework (WAF) project shows how to show a MessageBox without violating the MVVM pattern.

jbe
+2  A: 

Josh Smith also has a CodeProject article that might interest you here.

David Cuccia