Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Messageboxes in MVVM?

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.

like image 916
BigTiger Avatar asked May 21 '10 13:05

BigTiger


People also ask

How do I open a message box?

Open Settings . Click Apps. In the list of apps, click Messages.

What is Mvvm command?

Commands are an implementation of the ICommand interface that is part of the . NET Framework. This interface is used a lot in MVVM applications, but it is useful not only in XAML-based apps.

What is C# MVVM?

Introduction to MVVM C# MVVM (Model-View-ViewModel) C# is the technique of generating the client applications which control the core features of the WPF platform, enabling the ease of unit testing of app functionality. MVVM (Model-View-ViewModel) is an architectural pattern that signifies three different components.


1 Answers

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.

like image 63
Andreas Krebs Avatar answered Oct 10 '22 22:10

Andreas Krebs