Dialog messages launched from Viewmodel through Dialogcoordinator

Asked

Viewed 97 times

0

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

I am trying to take advantage of the "Dialogcoordinator" feature provided by "Mahapps.Metro" to trigger the messages dialog of my Viewmodels. However, by executing the "Showmessageasync" method the system is interrupting the execution without firing any Exception or message whatsoever. All configuration was performed according to the documentation and I cannot identify the reason why it is not working.

Follows related codes.

XAML attributes required:

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

Builder of 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;
        }

Thanks in advance for the help!

1 answer

0

I have identified the reason. It is so simple that it is silly. This relaycommand is being fired at my mainwindow’s closing event. Because the application is quite performative and the method is asynchronous, the event completed its operation in parallel before the message was fired.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.