Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM light sending message from ViewModel to View

Tags:

c#

mvvm

messenger

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.

like image 768
MTA Avatar asked Oct 21 '22 12:10

MTA


1 Answers

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);  
like image 54
Patryk Ćwiek Avatar answered Oct 24 '22 11:10

Patryk Ćwiek