If you are reading this, probably you know already that in MVVM we use services for displaying dialogs so that out code can stay loosely coupled and testable. A simple implementation of Dialog Service is given below:-
Interface:-
public interface IDialogService
{
MessageBoxResult ShowMessageBox(string content,
string title, MessageBoxButton buttons);
void ShowDialog(ViewModelBase viewModel, Window dialog);
ViewModelBase ViewModel { get; set; }
Window DialogWindow { get; set; }
}
Implementation:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using BP = BPCodeScreening.Infrastructure;
using BPCodeScreening.Infrastructure.Services;
namespace DialogServiceExample.Infrastructure.Services
{
public class DialogService : IDialogService
{
public ViewModelBase ViewModel { get; set; }
public Window DialogWindow { get; set; }
public MessageBoxResult ShowMessageBox(string content,
string title, MessageBoxButton buttons)
{
return MessageBox.Show(content, title, buttons);
}
public void ShowDialog(ViewModelBase viewModel, Window dialog)
{
DialogWindow = dialog;
ViewModel = viewModel;
dialog.Title = viewModel.Title;
dialog.DataContext = ViewModel;
dialog.Owner = Application.Current.MainWindow;
dialog.ShowInTaskbar = false;
dialog.ShowDialog();
}
}
}
Usage: A good way to use this is to ask the dependency injection container to give you the instance and then call ShowDialog() on it:-
var _dialogService = container.Resolve<DialogService>();
_dialogService.ShowDialog(new ModalMessageBoxViewModel { Title = "Information", Message = m }, new MyDialogView()));
Interface:-
public interface IDialogService
{
MessageBoxResult ShowMessageBox(string content,
string title, MessageBoxButton buttons);
void ShowDialog(ViewModelBase viewModel, Window dialog);
ViewModelBase ViewModel { get; set; }
Window DialogWindow { get; set; }
}
Implementation:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using BP = BPCodeScreening.Infrastructure;
using BPCodeScreening.Infrastructure.Services;
namespace DialogServiceExample.Infrastructure.Services
{
public class DialogService : IDialogService
{
public ViewModelBase ViewModel { get; set; }
public Window DialogWindow { get; set; }
public MessageBoxResult ShowMessageBox(string content,
string title, MessageBoxButton buttons)
{
return MessageBox.Show(content, title, buttons);
}
public void ShowDialog(ViewModelBase viewModel, Window dialog)
{
DialogWindow = dialog;
ViewModel = viewModel;
dialog.Title = viewModel.Title;
dialog.DataContext = ViewModel;
dialog.Owner = Application.Current.MainWindow;
dialog.ShowInTaskbar = false;
dialog.ShowDialog();
}
}
}
Usage: A good way to use this is to ask the dependency injection container to give you the instance and then call ShowDialog() on it:-
var _dialogService = container.Resolve<DialogService>();
_dialogService.ShowDialog(new ModalMessageBoxViewModel { Title = "Information", Message = m }, new MyDialogView()));
MyDialogView is just an empty window used to inject the DataTemplate of your viewModel. Do not forget to specify the data template for your dialog's view model in the app.xaml file:-
<DataTemplate DataType="{x:Type local:ModalMessageBoxViewModel}">
<views:MessageView/>
</DataTemplate>
Easy, Isn't it?
Thanks & Enjoy!