I'm very new to MVVM light and im having some problems using the Messenger..
Im trying to send a message from the ViewModel to the View but this is not working for me, I have tried going through posts and sample code but i think there is something very simple that i am missing.. i appreciate your help..
In my ViewModel I send a message
Messenger.Default.Send(new ClearNewProjectSettingsMessage());
This is my Message class, I dont really know what to put here because everything is in my View class
public class ClearNewProjectSettingsMessage
{
public ClearNewProjectSettingsMessage()
{
}
}
I register the message in the Views constructor:
Messenger.Default.Register<ClearNewProjectSettingsMessage>(
this,
() => ClearSettings()
);
This unfortunately does not compile so I tried this:
Messenger.Default.Register<ClearNewProjectSettingsMessage>(this,ClearSettings);
But it still does not work..
This is the method i want to call (in the view):
private void ClearSettings()
{
passwordBox.Clear();
}
Thanks.
Now I have not ever used MVVM Light Messenger, but as with any event aggregator/message bus, it makes sense so that the handler method takes in the object that it handles as an argument:
I have checked the source code and the registration looks as follows:
public virtual void Register<TMessage>(object recipient, Action<TMessage> action)
With that in mind, this definition:
private void HandleClearSettings(ClearNewProjectSettingsMessage message)
{
}
should fix your registration:
Messenger.Default.Register<ClearNewProjectSettingsMessage>(this, HandleClearSettings);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With