2013-10-10 127 views

回答

1

是的,所以我結束了與自己的包裝周圍信使。沒有聰明的解決方案,只是簡單的包裝。如果有其他人需要它:

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); 
    } 
} 
1

在抽象的ViewModel中創建一個每個人都從中繼承的函數,該函數使用一個定義或全局變量來確定它是否也被調試。如果您正在以調試或發佈的方式運行,這也可能是。

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

不是,太簡單了。我已經有大量的Send()調用,不想將它們全部更改爲SendMessageWithDebug()或其他東西。 –

+0

@VitaliiVasylenko然後,您需要重寫或包含和更改源代碼,這似乎違背了原文中的陳述。 – bland

+0

也許是某種擴展?也許我原來的陳述並不清楚。我的意思是,我正在尋找一種聰明的方式,這將允許我做最少量的更改。 –