Messages dialog fired the ViewModel through the DialogCoordinator using MahApps.Metro and MVVM Light Toolkit

I came across a problem working on a WPF project where I use "MVVM Light Toolkit" and "MahApps.Metro".

I'm trying to enjoy the feature "DialogCoordinator" provided by "MahApps.Metro" to trigger the dialog messages of my ViewModels. However, when performing the method "ShowMessageAsync" nothing is happening. The whole setup was performed according to the documentation and can not identify the reason to not be working.

The following related codes.

Required XAML attributes:

xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Dialog:DialogParticipation.Register="{Binding}"

ViewModelLocator builder 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>();
}

Builder MainViewModel:

public MainViewModel(IDialogCoordinator dialogCoordinator)
{
    _dialogCoordinator = dialogCoordinator;            
}

RelayCommand responsible for firing 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;
}

Upvotes: 2

Views: 974

Answers (1)

I identified the reason. It is so simple that it's silly. This RelayCommand being shot in the Closing event of my MainWindow. Because the application be quite performative and the method is asynchronous, the event concluded in parallel operation before the message was triggered.

Anyway, thank you for your attention!

Upvotes: 1

Related Questions