lbugnion / mvvmlight

The main purpose of the toolkit is to accelerate the creation and development of MVVM applications in Xamarin.Android, Xamarin.iOS, Xamarin.Forms, Windows 10 UWP, Windows Presentation Foundation (WPF), Silverlight, Windows Phone.
http://www.mvvmlight.net
MIT License
1.17k stars 312 forks source link

Can I see ALL messages being sent with MvvmLight? #56

Open MrYossu opened 5 years ago

MrYossu commented 5 years ago

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

abgenullt commented 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

MrYossu commented 5 years ago

@abgenullt Thanks for the reply. Can you give me some pointers as to where and how I would do this?

Thanks

abgenullt commented 5 years ago

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());

GiancarloLelli commented 5 years ago

@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

MrYossu commented 5 years ago

@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.

GiancarloLelli commented 5 years ago

maybe we should ask @lbugnion if this change is good for a PR!