I am currently building an application where I currently use MessageBox.Show()
in the following way in the ConnectionHandler
Model
,
if (MessageBox.Show("Question", "Window Title",
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
// User clicked yes
// do stuff...
}
else
{
// User clicked no
// do other stuff..
}
This is to initialize a connection to a backend.
But my issue is that according to MVVM you cannot modify/take input from view in this way.
So what I did then is that I followed the answer here: how to show Messagebox in MVVM
which led me to this in my MainViewModel
,
public event EventHandler<MvvmMessageBoxEventArgs> MessageBoxRequest;
protected void MessageBox_Show(Action<MessageBoxResult> resultAction, string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxOptions options = MessageBoxOptions.None)
{
if (this.MessageBoxRequest != null)
{
this.MessageBoxRequest(this, new MvvmMessageBoxEventArgs(resultAction, messageBoxText, caption, button, icon, defaultResult, options));
}
}
And then I listen to the MessageBoxRequest
in my View
Which kind of works, but I struggle to actually use it from my Model
in the same way I did before, since this solution takes a function that gets triggered when the MessageBox
is clicked away, instead of just locking, waiting for input then returning the data to be used in the if statement.
I am new to MVVM so please, if possible, explain it in more general programming terms, not MVVM specific ones.