Messenger is a class provided by mvvmlight toolkit. Used for communication.
Messenger.Default.Register<Customer>(this, "ChildWindow", AddCustomer);
To use messenger to send messages, you must register a message first. There is a token used to identify different messages, such as "childwindow" in the code above ". This indicates the receiver. <Customer> is the type of the message (the parameter type passed to addcustomer in a moment ).
Our messages can be of any type, either a set type or a simple boolean type.
Messenger. Default. Register
//// Abstract: register a message receiver. When a message is sent and received, run the action // parameter: // recipient: // message receiver. //// token: // like a channel, // when the token registered by the recipient is the same as the token sent by the sender, the message is accepted. // action: // The operation that will be performed after the message is sent. //// type parameter: // tmessage: // type of the message to be sent and the operation parameter to be executed. public Virtual void register <tmessage> (Object recipient, object token, Action <tmessage> action );
We use an okbutton in the view to bind a commandOkbuttoncommandTo send the message. This means that your mom has called you to addcustomer.
Public relaycommand <customer> okbuttoncommand {Get; private set;} private void okbuttonclick (customer PARAM) {/** send a customer message * token to childwindow. Only recipients with the same token will receive this message * // register a receiver token to childwindow messenger. default. send <customer> (Param, "childwindow ");}
We also need to define an addcustomer method for this to be executed after receiving the message.
Addcustomer
Do not forget to cancel the message in cleanup. Otherwise, you may add more data when your mom calls addcustomer. Please use
Messenger.Default.Unregister<Customer>(this, "ChildWindow", AddCustomer);