1
votes

Im using MVVM Light Messenger in my WPF application, and something is not working as expected.

my view-model registered with a token. im using 'long' objects as tokens. my code registered for example with the token 5, then sends request to a service. when the service replies it handles in my second view-model, which will then send the message with the same token.

When i debug and print the registration and sending of the messages it seems OK, but for some reason not all the messenger are received by the registered.

My registration and handling looks as follows:

private void registerTest()
{
    long tokenId = getNextToken();
    ExtraData data = new ExtraData();
    Messenger.Default.Register<MyMsg>(this, tokenId, (m) => recieve(m,data));
}

private void receive(MyMsg m,ExtraData data)
{
    Messenger.Default.Unregister<MyMsg>(this, m.tokenId);
}

My sending looks as follows:

private void sendTest(long tokenId)
{
    Messenger.Default.Send(new MyMsg(tokenId), tokenId);
}

I always register with token X before its received in my sendTest, but for some reason, sometimes when sendTest(X) is called, its not received.

Anyone has any idea whats going on?

1
What does the rest of your class look like? How are you injecting the messenger?C Bauer
the problems appears to be in the lambda in the action, when not using local variable in the lambda this will work. i found a related example here linkGaby Rubin
Oh! I think I see what you're talking about...C Bauer

1 Answers

0
votes

You should have your ExtraData as a class property on your message to be able to interact with it from different sources.

public class MyMsg {
    public int TokenId {get;set;}
    public ExtraData Data {get;set;}
}


public void registerTest()
{
    Messenger.Default.Register<MyMsg>(this, recieve);
}

public void recieve(MyMsg myMsg)
{
    Messenger.Default.Unregister<MyMsg>(this, myMsg.TokenId);
    //Note that you can also access m.Data here if you need to
}


public void sendTest()
{
    var myMsg = new MyMsg {TokenId = GetNextToken(), Data = new ExtraData()};
    Messenger.Default.Send(myMsg);
}