title: Send a message from View to ViewModel : what is an appropriate token?
body: <p>Lets define :</p>
  
  <ul>
  <li>a viewModel : the <code>TabViewModel</code> class </li>
  <li>a view : the <code>TabView</code> class</li>
  </ul>
  
  <p>I have <em>n</em> instances of the <code>TabView</code> class, and so <em>n</em> instances of <code>TabViewModel</code>.
  When one instance of the <code>TabView</code> class send a message, I want it to be received by its own viewmodel, <strong>and only this one</strong>.</p>
  
  <p>As I understand the Messenger of the mvvm light toolkit, I whould use something like :</p>
  
  <pre><code>// in the view
  Messenger.Default.Send(new RefreshMessage(/*...*/), oneToken);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>// in the viewmodel 
  Messenger.Default.Register&lt;RefreshMessage&gt;(this, oneToken, MyViewModelMethod);
  </code></pre>
  
  <p><strong>What should I use for <code>oneToken</code> ?</strong></p>
  
  <p>My first thought was to use the ViewModel instance as token :</p>
  
  <pre><code>// in the view
  Messenger.Default.Send(new RefreshMessage(/*...*/), this.DataContext);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>// in the viewmodel 
  Messenger.Default.Register&lt;RefreshMessage&gt;(this, **this**, MyViewModelMethod);
  </code></pre>
  
  <p>This seems "mvvm-friendly" to me, because the view doesn't know what is the DataContext.
  But with this solution, I fear a memory leak : in mvvm light, the recipients are weak-referenced, but the token is not (as you will see in the <a href="http://bit.ly/1cPxMyV" rel="nofollow">WeakActionAndToken struct of the Messenger class</a>. </p>
  
  <p><strong>What can I use as token ? Is the viewmodel instance a good choice, and how can I prevent memory leak if I use it ?</strong></p>
  
  <hr>
  
  <h2><em><strong>EDIT : Possible SOLUTIONS</em></strong></h2>
  
  <p><strong>Option 1 (based on <em>ethicallogics</em> answer):</strong></p>
  
  <ol>
  <li>Define a Token property (of type string or GUID for example) on both the view and the viewmodel</li>
  <li>Define the value of one of them (a unique value, set it for example in the constructor of the viewmodel)</li>
  <li>Bind them together in XAML</li>
  <li>Use them in the Messenger call</li>
  </ol>
  
  <p><strong>Option 2 (the one I've taken) :</strong></p>
  
  <p>Use the viewmodel instance as Token. </p>
  
  <p>To prevent a memory leak, we must encapsulate it in a weakReference. In order to work with the Messenger, which compares 2 tokens, the weakReference shoud have the <code>Equals</code> method implemented (which is not the case of the default .Net implementation of the <code>WeakReference</code> class).</p>
  
  <p>So we have :</p>
  
  <pre><code>// in the view
  Messenger.Default.Send(new RefreshMessage(), new EquatableWeakReference(this.DataContext));
  </code></pre>
  
  <p>and</p>
  
  <pre><code>// in the viewmodel 
  Messenger.Default.Register&lt;RefreshMessage&gt;(this, new EquatableWeakReference(this), ApplyRefreshMessage);
  </code></pre>
  
  <p>I implemented the <code>EquatableWeakReference</code> class as follow :</p>
  
  <pre><code>/// &lt;summary&gt;
  /// A weak reference which can be compared with another one, based on the target comparison.
  /// &lt;/summary&gt;
  public class EquatableWeakReference : IEquatable&lt;EquatableWeakReference&gt;
  {
      private WeakReference reference;
      private int targetHashcode;
  
      public EquatableWeakReference(object target)
      {
          if (target == null)
              throw new ArgumentNullException("target");
          reference = new WeakReference(target);
          targetHashcode = target.GetHashCode();
      }
  
      public override bool Equals(object obj)
      {
          return Equals(obj as EquatableWeakReference);
      }
  
      /// &lt;summary&gt;
      /// As Equals is overriden, we must provide an override for GetHashCode.
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public override int GetHashCode()
      {
          return targetHashcode;
      }
  
      public bool Equals(EquatableWeakReference other)
      {
          if (other == null
              || !reference.IsAlive
              || !other.reference.IsAlive)
              return false; // we assume that if both references are not alive, the result is inconclusive : let's say false.
          return this.reference.Target.Equals(other.reference.Target);
      }
  }
  </code></pre>
  
  <p>Advantage is a lightweight code on both the view and the viewmodel, with no memory leak. Tested successfully.
  Feel free to comment if you have a better solution.</p>