title: mvvm light Messenger.Default.Register in View codebehind?
body: <p>The <strong>BookShelf</strong> solution <strong>John Papa</strong> presented at <strong>Mix11</strong> has something that sounds a bit odd to me...
  It uses an <strong>MVVM</strong> pattern and <strong>MVVM Light toolkit</strong>... Everything is great.
  The only thing <strong>I can't understand</strong> is this:
  <strong>In codebehind of Views it register for a couple of messages</strong>, here it is the code:</p>
  
  <pre><code>public partial class BookView : Page
  {
      public BookView()
      {
          InitializeComponent();
          //btnEdit.SetBinding(Button.IsEnabledProperty, new Binding("User.IsAuthenticated") { Source = Application.Current.Resources["WebContext"] });
          Title = ApplicationStrings.HomePageTitle;
          RegisterMessages();
      }
  
      private void RegisterMessages()
      {
          Messenger.Default.Register&lt;LaunchEditBookMessage&gt;(this, OnLaunchEditBook);
          Messenger.Default.Register&lt;SavedBookDialogMessage&gt;(this, OnSaveBookDialogMessageReceived);
      }
  
      private void OnLaunchEditBook(LaunchEditBookMessage msg)
      {
          var editBook = new EditBookWindow();
          editBook.Show();
      }
  
      private void OnSaveBookDialogMessageReceived(SavedBookDialogMessage msg)
      {
          MessageBox.Show(msg.Content, msg.Caption, msg.Button);
      }
  //...
  </code></pre>
  
  <p>It is a <strong>business application</strong>, if you switch from that Page to another and then come back there, <strong>the page gets instanciated again and keeps registering</strong> for those messages causing those to <strong>fire multiple times</strong>...</p>
  
  <p><strong>How comes</strong> it subscribe for those messages in codebehind instead of in ViewModels? has this something to do with UI thread? **Is this a correct implementation?</p>
  
  <p><strong>How would you unregister those messages if user navigates to another page?</strong></p>
  
  <p><strong>EDIT:</strong> REFACTORED CODE</p>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;sdk:Page Loaded="Page_Loaded" Unloaded="Page_Unloaded"&gt;
  </code></pre>
  
  <p><strong>CODE BEHIND</strong></p>
  
  <pre><code>private void Page_Loaded(object sender, RoutedEventArgs e)
      {
          RegisterMessages();
      }
  
      private void Page_Unloaded(object sender, RoutedEventArgs e)
      {
          Messenger.Default.Unregister(this);
      }
  </code></pre>