Personally, I would go with option 3.
The messaging engine keeps your view modes separate from each other, and as soon as you work with one example, you will see that it is quite simple.
Personally, I like to add a class of message broker with static methods for each type of message I want to send, this helps me centralize the changes - but essentially you have send and receive. You can send what you want, and if something wants to receive it, they can.
MVVM Light is a great foundation for this.
Send:
GalaSoft.MvvmLight.Messaging.Messenger.Send<LoginSuccessMessage>(new LoginSuccessMessage() { UserName = user });
Get in my target view the model constructor:
this.MessengerInstance.Register<LoginSuccessMessage>(this, this.OnLoginSuccessMessage);
Handler in the target view model:
private async void OnLoginSuccessMessage(LoginSuccessMessage message) { this.CurrentUserName = message.UserName; this.MoveToState(ApplicationViewModelState.Active); await Task.Delay(5000); this.MoveToState(ApplicationViewModelState.Idle); }
In this example, I am sending the user ID as a property in the message class:
public class LoginSuccessMessage : MessageBase { private string _UserName; public string UserName { get { return this._UserName; } set { this._UserName = value; } } }
Replace this property with what you want to assemble or a complex object.
source share