Git Product home page Git Product logo

Comments (6)

GiancarloLelli avatar GiancarloLelli commented on May 18, 2024 2

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

from mvvmlight.

abgenullt avatar abgenullt commented on May 18, 2024

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

from mvvmlight.

MrYossu avatar MrYossu commented on May 18, 2024

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

Thanks

from mvvmlight.

abgenullt avatar abgenullt commented on May 18, 2024

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

from mvvmlight.

GiancarloLelli avatar GiancarloLelli commented on May 18, 2024

@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

from mvvmlight.

MrYossu avatar MrYossu commented on May 18, 2024

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

from mvvmlight.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.