Blade MVVMLight 9: Messenger, mvvmlightmessenger
One of the goals of MVVM is to decouple View and ViewModel. View is responsible for View display, and ViewModel is responsible for business logic processing. View. xaml. cs is concise and does not contain complex business logic code.
However, in actual situations, the interaction between View and ViewModel is still complicated. The separation of View and ViewModel is not as clearly defined.
For example, the following two scenarios:
1. If you want a view page to pop up a dialog box, subforms, processing interface elements, and playing animations. If these operations are placed in ViewModel, The ViewModel still needs to process View-level elements, resulting in the dependency between View and ViewModel.
The best way is to notify the ViewModel of what the View should do, and the View listener receives the command and processes the interface.
2. Communication between ViewModel and ViewModel is also required through message transmission.
The MVVM Light Messenger class provides the ability to solve the above two problems:
The Messenger class is used for application communication. The receiver can only accept the registered message type. In addition, the target type can be specified and implemented using the Send <TMessage, TTarget> (TMessage message,
In this case, information can only be transmitted. If the receiver type matches the target parameter type, the message can be any simple or complex object, you can use a specific message type or create your own type to inherit from them.
The main interaction modes of the Messager class are information receiving and sending (which can be understood as "Publish Message Service" and "subscribe to message service"). do you think of the observer mode? Hahaha.
MVVM Light Messenger aims to streamline this scenario through a simple design mode: Any object can be the receiver, any object can be the sender, and any object can be a message.
1. Message interaction between View and ViewModel
Registering a message generator in View and ViewModel is equivalent to subscribing to the service. Contains the message flag, message parameters, and message execution method. As follows:
Message flag token: ViewAlertTo identify and only read the messages sent by one or some Sender, and perform corresponding processing. Therefore, the token of the Sender must be consistent.
Execution method Action: ShowReceiveInfoIs used to execute subsequent work after the message is received. Note that the wildcard capability is supported here, so passing parameters is very convenient.
View. xaml. cs code:
1 public partial class NessagerForView: Window 2 {3 public NessagerForView () 4 {5 InitializeComponent (); 6 7 // message flag token: ViewAlert, it is used to identify and only read messages sent by one or some Sender and perform corresponding processing. Therefore, the token on the Sender side must be consistent. // The Action: ShowReceiveInfo is executed, it is used to perform subsequent work after the message is received. Note that the wildcard capability is supported here, so it is convenient to transmit parameters. 9. Messenger. default. register <String> (this, "ViewAlert", ShowReceiveInfo); 10 this. dataContext = new MessengerRegisterForVViewModel (); 11 // Uninstall all MVVMLight messages registered with the current (this) object 12 this. unloaded + = (sender, e) => Messenger. default. unregister (this); 13} 14 15 /// <summary> 16 /// subsequent work after receiving the message: pop-up message box 17 // </summary> 18 // <param name = "msg"> </param> 19 private void ShowReceiveInfo (String msg) based on the returned information) 20 {21 MessageBox. show (msg); 22} 23}
ViewModel code:
1 public class MessengerRegisterForVViewModel: ViewModelBase 2 {3 4 public MessengerRegisterForVViewModel () 5 {6 7} 8 9 # region Command 10 11 private RelayCommand sendCommand; 12 /// <summary> 13 /// send command 14 /// </summary> 15 public RelayCommand SendCommand16 {17 get18 {19 if (sendCommand = null) 20 sendCommand = new RelayCommand () => ExcuteSendCommand (); 21 return sendCommand; 22 23} 24 set {sendCommand = value;} 25} 26 27 private void ExcuteSendCommand () 28 {29 Messenger. default. send <String> ("ViewModel Notification View pop-up message box", "ViewAlert"); // note: the token parameter is 30} 31 32 # endregion33}
Result:
2. Message interaction between ViewModel and ViewModel. In many scenarios, ViewModel and ViewModel also need to communicate through message transmission.
For example, I open two views, one is the user information list, and the other is the user information add page. If you want to achieve the added information, the user information list view is refreshed in real time, message notification is undoubtedly a great experience.
Let's simulate:
MessengerRegisterViewModel code:
1 public class MessengerRegisterViewModel: ViewModelBase 2 {3 public MessengerRegisterViewModel () 4 {5 // Messenger: Messenger 6 // Recipient: Recipient 7 Messenger. default. register <String> (this, "Message", ShowReceiveInfo); 8} 9 10 11 # region attribute 12 13 private String receiveInfo; 14 /// <summary> 15 /// receives the value passed by the messenger 16 /// </summary> 17 public String ReceiveInfo18 {19 get {return receiveInfo ;} 20 set {receiveInfo = value; RaisePropertyChanged () => ReceiveInfo);} 21} 22 23 # endregion24 25 26 # region launch new window 27 28 private RelayCommand showSenderWindow; 29 30 public RelayCommand ShowSenderWindow31 {32 get {33 if (showSenderWindow = null) 34 showSenderWindow = new RelayCommand () => ExcuteShowSenderWindow (); 35 return showSenderWindow; 36 37} 38 set {showSenderWindow = value;} 39} 40 41 private void ExcuteShowSenderWindow () 42 {43 MessengerSenderView sender = new MessengerSenderView (); 44 sender. show (); 45} 46 47 # endregion 48 49 50 # region auxiliary function 51 // <summary> 52 // display the Recipient Information 53 /// </summary> 54 /// <param name = "msg"> </param> 55 private void ShowReceiveInfo (String msg) 56 {57 ReceiveInfo + = msg + "\ n"; 58} 59 # endregion60}
MessengerSenderViewModel code:
1 public class MessengerSenderViewModel: ViewModelBase 2 {3 public MessengerSenderViewModel () 4 {5 6} 7 8 # region attribute 9 private String sendInfo; 10 /// <summary> 11 /// send message 12 /// </summary> 13 public String SendInfo14 {15 get {return sendInfo;} 16 set {sendInfo = value; raisePropertyChanged () => SendInfo);} 17} 18 19 # endregion20 21 # region Command 22 23 private RelayCommand sendCommand; 24 /// <summary> 25 /// send command 26 /// </summary> 27 public RelayCommand SendCommand28 {29 get30 {31 if (sendCommand = null) 32 sendCommand = new RelayCommand () => ExcuteSendCommand (); 33 return sendCommand; 34 35} 36 set {sendCommand = value;} 37} 38 39 private void ExcuteSendCommand () 40 {41 Messenger. default. send <String> (SendInfo, "Message"); 42} 43 44 # endregion45}
The result is as follows:
Download Sample Code
Reprinted please indicate the source, thank you