Passing a parameter from one view model to another using a messenger

When I want to open a new window from the View model, I usually use a messenger. But now I want to open a new window from the view model and transfer the object from the calling view model to the called view model. How can i implement this? In my viewmodelbase class, I currently have the following methods.

public void SendNotificationMessage(string notification) { Messenger.Default.Send<NotificationMessage>(new NotificationMessage(notification)); } public void SendNotificationMessageAction(string notification, Action<object> callback) { var message = new NotificationMessageAction<object>(notification, callback); Messenger.Default.Send(message); } 

Please help me

+4
source share
1 answer

Your syntax will look something like this:

 //Subscribe Messenger.Default.Register<OpewNewWindowMessage>(OpenNewWindowMethod); // Broadcast var message = new OpewNewWindowMessage(); message.ViewModel = this; Messenger.Default.Send<OpewNewWindowMessage>(message); // Subscribed method would look like this void OpenNewWindowMethod(OpewNewWindowMessage e) { // e.ViewModel would contain your ViewModel object } 

In the above example, you must create a new class named OpewNewWindowMessage and assign the ViewModel property to it, then you must fill this value before sending the message.

OpenNewWindowMethod() will receive a message and will be able to access OpewNewWindowMessage.ViewModel to access the ViewModel property

Technically, you don’t need to create a message object if you bypass only one property, but usually I find it makes it easier to read and maintain the code if you create a message object instead of using a common <object> like you have in your code.

+3
source

Source: https://habr.com/ru/post/1469232/


All Articles