Dialog service is an approach where a layer of abstraction is used to show dialog boxes. View models delegate the responsibility of showing the dialogs to a dialog service and simply provide the service the data needed for display. The dialog service owns the responsibility of showing dialogs and we are able to keep our view models decoupled from
System.Windows and avoid the need for references from our views to our view models. In unit tests, we can inject a fake dialog service instance instead of showing the actual dialog and use our fake object for stubbing and mocking.
Let's consider a simple example WPF
MVVMLight project to show a dialog box using dialog service. The following view has a
TextBlock and a
Button. Clicking on the button should display a dialog box.
The XAML definition of the view can be as follows:
<Window x:Class="MVVMBasedDialogs.MainWindowDialogService" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http:/...