MVVM Light Messenger

215 Views Asked by At

I am new to MVVM Light and I am trying to learn and implement the Messenger class. I have read many other messenger tutorials and posts but still can't get it to work. Below is some code I wrote to help me learn. From my research I think I have everything implemented correctly but it does not work. Does anyone know what I am doing wrong?

public class MainViewModel : ViewModelBase
{
    public MainViewModel() { }  

    private RelayCommand _sendMessage;  
    public RelayCommand SendMessage
    {
        get
        {
            return _sendMessage
                ?? (_sendMessage = new RelayCommand(
                                      () =>
                                      {
                                          var pInfo = new PersonalInfo { Name = "Some Name", Age = 31 };
                                          Messenger.Default.Send(pInfo);                                            
                                      }));
        }
    }

} 

public class MessageReciever
{
    public MessageReciever()
    {          
        Messenger.Default.Register<PersonalInfo>(this,
            action =>
            {
                Debug.WriteLine(action.Age);
                Debug.WriteLine(action.Name);
            }
            );
    }    
}

public class PersonalInfo
{
    public string Name { get; set; }
    public int Age { get; set; }
}
2

There are 2 best solutions below

1
On BEST ANSWER

Try this:

public class MainViewModel : ViewModelBase
{
    MessageReciever mr = new MessageReciever();

    public MainViewModel() { }

    private RelayCommand _sendMessage;
    public RelayCommand SendMessage {
        get {
            return _sendMessage
                ?? (_sendMessage = new RelayCommand(
                                      () =>
                                      {
                                          var pInfo = new PersonalInfoMessage(new PersonalInfo { Name = "Some Name", Age = 31 } );
                                          Messenger.Default.Send(pInfo);
                                      }));
        }
    }
}

public class MessageReciever
{
    public MessageReciever() {
        Messenger.Default.Register<PersonalInfoMessage>(this,
            action =>
            {
                Debug.WriteLine(action.Content.Age);
                Debug.WriteLine(action.Content.Name);
            }
            );
    }
}

public class PersonalInfo
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class PersonalInfoMessage : GenericMessage<PersonalInfo>
{
    public PersonalInfoMessage(PersonalInfo content)
        : base(content) {
    }
}
0
On

How are making sure the message receiver is instantiated? You might want to create an instance in your sending view model - just for learning.

*Typically, you use messenger for view models to 'talk to each other'. So the best way to learn it is to have two controls with separate view models.

If you make your message receiver extend baseviewmodel and bind a new control to it in your application might make it more obvious what is going on.*