MvvmLight print message to debug

女生的网名这么多〃 提交于 2020-01-06 03:52:17

问题


Is there a simple way to print all Messenger.Default.Send() to debug? Don't want to override them.


回答1:


Create a function in an abstract ViewModel that everyone inherits from, which uses a define or global variable to determine if it debugs as well/instead. This could also be if you're running as Debug or Release.

public abstract BaseViewModel
    public SendMessage()
    {
        if(DEBUG) // The global variable, or definition, or current run type - whatever
            Debug.WriteLine();
        // else // Commented if you want to always send, uncomment if you want to send instead.
        Messenger.Default.Send();
    }


public RealViewModel : BaseViewModel
...
    SomeMethod()
    {
        ...
        base.SendMessage();



回答2:


Yep, so i ended up with own wrapper around Messenger. No smart solutions, just straightforward wrapping. In case, anybody else would need it:

public static class MvvmLightMessenger
{
    public static void Register<TMessage>(object recipient, Action<TMessage> action)
    {
        Messenger.Default.Register(recipient,action);
    }

    public static void Register<TMessage>(object recipient, bool receiveDerivedMessagesToo, Action<TMessage> action)
    {
        Messenger.Default.Register(recipient, receiveDerivedMessagesToo, action);
    }

    public static void Register<TMessage>(object recipient, object token, Action<TMessage> action)
    {
        Messenger.Default.Register(recipient, token, action);
    }

    public static void Register<TMessage>(object recipient, object token, bool receiveDerivedMessagesToo, Action<TMessage> action)
    {
        Messenger.Default.Register(recipient, token, receiveDerivedMessagesToo, action);
    }





    public static void Send<TMessage>(TMessage message)
    {
        Debug.WriteLine("{!} Message: " + message);
        Messenger.Default.Send<TMessage>(message);
    }

    public static void Send<TMessage, TTarget>(TMessage message)
    {
        Debug.WriteLine("{!} Message: " + message + " to target: " + typeof(TTarget));
        Messenger.Default.Send<TMessage, TTarget>(message);
    }

    public static void Send<TMessage>(TMessage message, object token)
    {
        Debug.WriteLine("{!} Message: " + message + " token: " + token);
        Messenger.Default.Send<TMessage>(message, token);
    }







    public static void Unregister<TMessage>(object recipient)
    {
        Messenger.Default.Unregister<TMessage>(recipient);
    }

    public static void Unregister<TMessage>(object recipient, Action<TMessage> action)
    {
        Messenger.Default.Unregister<TMessage>(recipient, action);
    }

    public static void Unregister<TMessage>(object recipient, object token)
    {
        Messenger.Default.Unregister<TMessage>(recipient, token);
    }

    public static void Unregister<TMessage>(object recipient, object token, Action<TMessage> action)
    {
        Messenger.Default.Unregister<TMessage>(recipient, token, action);
    }
}


来源:https://stackoverflow.com/questions/19301887/mvvmlight-print-message-to-debug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!