dialog messages triggered from the ViewModel via the DialogCoordinator

0

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!

    
asked by anonymous 24.02.2016 / 17:37

1 answer

0

I have identified the reason. It's so simple that it sucks. This RelayCommand is being triggered in the Closing event of my MainWindow. Because the application is fairly performative and the method is asynchronous, the event concludes its operation in parallel before the message is triggered.

    
25.02.2016 / 12:16