问题
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