Open MrYossu opened 5 years ago
You could override the default messenger (Messenger.OverrideDefault) with your own implementation of IMessenger that just forwards the method calls to an internal Messenger object and add some custom actions...
Regards, abgenullt
@abgenullt Thanks for the reply. Can you give me some pointers as to where and how I would do this?
Thanks
You could create a class like this:
public class MyMessenger : IMessenger
{
/// <summary>
/// Messenger, that will do the real work.
/// </summary>
private readonly Messenger _internalMessenger;
/// <summary>
/// This method will be called from the send methods.
/// </summary>
private void CustomeSendAction<TMessage>(TMessage msg)
{
Console.WriteLine($"Message: {msg} sent.");
}
public MyMessenger()
{
_internalMessenger = new Messenger();
}
public void Register<TMessage>(object recipient, Action<TMessage> action, bool keepTargetAlive = false)
{
_internalMessenger.Register(recipient, action, keepTargetAlive);
}
public void Register<TMessage>(object recipient,
object token,
Action<TMessage> action,
bool keepTargetAlive = false)
{
_internalMessenger.Register(recipient, token, action, keepTargetAlive);
}
public void Register<TMessage>(object recipient,
object token,
bool receiveDerivedMessagesToo,
Action<TMessage> action,
bool keepTargetAlive = false)
{
_internalMessenger.Register(recipient, token, receiveDerivedMessagesToo, action, keepTargetAlive);
}
public void Register<TMessage>(object recipient,
bool receiveDerivedMessagesToo,
Action<TMessage> action,
bool keepTargetAlive = false)
{
_internalMessenger.Register(recipient, receiveDerivedMessagesToo, action, keepTargetAlive);
}
public void Send<TMessage>(TMessage message)
{
CustomeSendAction(message);
_internalMessenger.Send(message);
}
public void Send<TMessage, TTarget>(TMessage message)
{
CustomeSendAction(message);
_internalMessenger.Send<TMessage, TTarget>(message);
}
public void Send<TMessage>(TMessage message, object token)
{
CustomeSendAction(message);
_internalMessenger.Send(message, token);
}
public void Unregister(object recipient)
{
_internalMessenger.Unregister(recipient);
}
public void Unregister<TMessage>(object recipient)
{
_internalMessenger.Unregister(recipient);
}
public void Unregister<TMessage>(object recipient, object token)
{
_internalMessenger.Unregister<TMessage>(recipient, token);
}
public void Unregister<TMessage>(object recipient, Action<TMessage> action)
{
_internalMessenger.Unregister(recipient, action);
}
public void Unregister<TMessage>(object recipient, object token, Action<TMessage> action)
{
_internalMessenger.Unregister(recipient, token, action);
}
}
.. And add this line to the static constructor of the ViewModelLocator replace the default messenger:
Messenger.OverrideDefault(new MyMessenger());
@abgenullt @MrYossu What about a simpler approach? I've forked the repo and added a method called AddVerboseMessageHandler
that takes in input an Action<object>
and only when debugging and when set the messenger invokes it passing the TMessage
as input. See commit
@GiancarloLelli Thanks for that, that was very nice of you!
How do I use this fork though? We have MVVMLight installed as a Nuget package, we don't include the source in our code, so I can't just replace that one file.
Thanks again.
maybe we should ask @lbugnion if this change is good for a PR!
I have a WPF application that uses MvvmLight. Messages are sent around at various points, and I would like to see (for debugging purposes, not for production code) whenever a message is sent, irrespective of where it originated.
Is this possible? I realise that I could pull down the source code for the library, use that instead of the actual DLLs, and set some breakpoints or similar, but that would be a huge amount of work. I'm hoping there is an easier way.
Thanks