Home > other >  MessageDialog Displaying Twice in WPF C# MVVM App
MessageDialog Displaying Twice in WPF C# MVVM App

Time:01-02

I am working on a WPF (MVVM) app. I have a message dialog that I am displaying. It should only display once, but is displaying twice. This dialog is within an OnCanSaveExecute() method that does some validation to enable a button.

if (!_overUnderPayment)
                            {
                                MessageDialogResult result = _messageDialogService.ShowOkCancelDialog($"Text goes here",
                    "Proceed?");
                                if (result == MessageDialogResult.OK)
                                {
                                    _overUnderPayment = true;
                                    return true;
                                }
                                else
                                {
                                    _overUnderPayment = true;
                                    return false;
                                }
                            }

_overUnderPayment starts off as false. As you can see I am setting it true within the conditional logic.

_messageDialogService is...

public MessageDialogResult ShowOkCancelDialog(string text, string title)
        {
            MessageBoxResult result = MessageBox.Show(text, title, MessageBoxButton.OKCancel, MessageBoxImage.Hand);
            return result == MessageBoxResult.OK
              ? MessageDialogResult.OK
              : MessageDialogResult.Cancel;
        }

The code runs and the dialog displays correctly. I can click 'Ok' or 'Cancel' and it goes away, then it displays again.

I stepped through the code using breakpoints and the first time it runs, it does it exactly as designed and hits the return statement and ends the method.

It then jumps back into my if statement above (even though _overUnderPayment is true) and redisplays.

I want to know why it is displaying twice please.

CodePudding user response:

The issue I was facing was a race condition. Since I am using MVVM and async/await, it was causing me an issue since the message service is not async/awaitable.

So before the boolean was being set to not display the dialog again, the code was running through the other conditions. This code is within my OnCanSaveExecute method which does several validation checks to enable the save button through the delegate command.

Since the message service is not async/awaitable, I eliminated my race condition by moving this dialog out of my OnCanSaveExecute method. I moved it to my OnSaveExecute method. This method bets triggered when the save button is clicked. I check the return from the end user. If the user selects "Cancel", I return out of the method to stop the method from processing the save. If they click "Ok", the method continues.

I searched and read through the docs and did not see any way to utilize async/await with messages, which makes sense since they are synchronous by nature. If anyone has done this successfully, I would be interested in their approach.

  • Related