Search code examples
c#mvvmcatel

IMessageMediator doesn't hit breakpoint


I've recently upgraded an old project from Catel 4.3 to Catel 5.0.

The project used the InterestedIn attribute which is no longer supported. I tried replacing it with the IMediator but can't get it to work.

In the ViewModel that Listens I have the following code:

private IMessageMediator _messageMediator;

public CustomerControlViewModel(IMessageMediator messageMediator)
{
    Argument.IsNotNull(() => messageMediator);
    _messageMediator = messageMediator;
}

[MessageRecipient(Tag = "AddCustomerWindowViewModel")]
protected void AddCustomerWindowViewModelExecuted(string value)
{
    if (value == "OnAccept")
        RetrieveCustomers();
}

And in the sending ViewModel I have:

public AddCustomerWindowViewModel(IMessageMediator messageMediator)
{
        Argument.IsNotNull(() => messageMediator);
        _messageMediator = messageMediator;
}

private void OnAccept()
{

    _messageMediator.SendMessage("OnAccept","AddCustomerWindowViewModel");

}

But when I place a breakpoint on the listener it never hits

I'm missing something, but what?


Solution

  • Just checked the code and unit tests for you.

    1. I recommend that you use constants for your tags (to prevent any typos)
    2. You should register the recipient as you can see here:

    https://github.com/Catel/Catel/blob/develop/src/Catel.Tests/Messaging/MessageMediatorFacts.cs#L199

    From what I remember, the view models in Catel should auto-register itself as recipients, but maybe we removed it for performance reasons (opt-in model). Couldn't find anything in the release notes though.

    Anyway, try registering (and don't forget to unregister!) the vm manually (I recommend in InitializeAsync and CloseAsync).