4

Whenever I want to open a new window from a View model, normally I am using messenger . But now I want to open a new window from a view model and pass an object from calling view model to called view model . How can I implement this? In my viewmodelbase class currently I am having 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

Jehof
  • 34,674
  • 10
  • 123
  • 155
Ranish
  • 877
  • 2
  • 10
  • 30

1 Answers1

3

Your syntax would 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 would create a new class called OpewNewWindowMessage and give it a property of ViewModel, then you would populate that value before broadcasting the message.

The OpenNewWindowMethod() would receive the message, and could access OpewNewWindowMessage.ViewModel to access the ViewModel property

Technically you don't need to create a message object if you're only passing around one property, however I usually find it makes the code easier to read and maintain if you create a message object instead of using a generic <object> like you have in your code.

Rachel
  • 130,264
  • 66
  • 304
  • 490