Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MVVM Foundation Messenger to Show Dialog

I'm building a WPF app and trying to conform to MVVM best practices. I'm using the MVVM Foundation framework and noticed the Messenger class, which I've read should be used for handling dialogs in WPF. This sounds great, but I'm totally not understanding how to use a Messenger for this purpose. Literally, all I want to do is open a modal About dialog --I don't need to pass any messages back and forth.

Was the intent of the Messenger class to be used for cases where dialogs require a message from its parent, or return a message to its parent? Is it overkill for an About dialog? Would I be better off simply adding code to an event handler to show the dialog?

like image 516
senfo Avatar asked Feb 28 '23 00:02

senfo


1 Answers

The idea behind the messaging pattern doesn't specifically have anything to do with showing dialogs. The idea is simply to provide a decoupled way to communicate between ViewModels.

You can leverage this infrastructure to solve your problem but you will have to implement the showing of the dialog yourself.

As Phillip showed above you can send messages between ViewModels. When your ViewModel receives the message it can set it's own internal property, say "ShowDialog", to true.

You can then have a binding that reacts to this property change operation and opens a dialog.

I have also built a simple messaging framework for the MVVM pattern that borrows from Josh's idea (and several other existing frameworks) you can read about it here

like image 199
Brad Cunningham Avatar answered Mar 07 '23 02:03

Brad Cunningham