I came across a problem working on a WPF project where I use "MVVM Light Toolkit" and "MahApps.Metro".
I'm trying to take advantage of the "DialogCoordinator" feature provided by "MahApps.Metro" to trigger dialog messages from my ViewModels. However, when you run the "ShowMessageAsync" method the system is halting the execution without firing any Exception or message whatsoever. All the configuration was done according to the documentation and I can not identify why it was not working.
Follow related codes.
Required XAML attributes:
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Dialog:DialogParticipation.Register="{Binding}"
ViewModelLocator constructor registering the DialogCoordinator used by MainViewModel:
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
}
else
{
SimpleIoc.Default.Register<IDataService, DataService>();
}
SimpleIoc.Default.Register<IDialogCoordinator, DialogCoordinator>();
SimpleIoc.Default.Register<MainViewModel>();
}
MainViewModel constructor:
public MainViewModel(IDialogCoordinator dialogCoordinator)
{
_dialogCoordinator = dialogCoordinator;
}
RelayCommand responsible for triggering the message:
public RelayCommand<CancelEventArgs> ClosingWindow
{
get
{
return _closingWindow
?? (_closingWindow = new RelayCommand<CancelEventArgs>(ExecuteClosingWindow));
}
}
private RelayCommand<CancelEventArgs> _closingWindow;
private async void ExecuteClosingWindow(CancelEventArgs e)
{
if (!IsQuitConfirmation) return;
var result = await _dialogCoordinator.ShowMessageAsync(this, "Teste", "Teste", MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings
{
AffirmativeButtonText = "OK",
NegativeButtonText = "CANCELAR",
AnimateShow = true,
AnimateHide = false
});
if (result == MessageDialogResult.Negative)
e.Cancel = true;
}
Thank you in advance for the help!