Mvvm light Messenger.Default.Register in View codebehind?

The BookShelf John Papa solution introduced in Mix11 has something strange for me ... It uses the MVVM pattern and the MVVM Light toolkit . All perfectly. The only thing I can’t understand : In codebehind of Views, it is registered for a couple of posts , here it is:

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<LaunchEditBookMessage>(this, OnLaunchEditBook);
        Messenger.Default.Register<SavedBookDialogMessage>(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);
    }
//...

This is a business application , if you switch from this page to another, and then return there, the page receives confirmation again and continues to register for messages that cause these to fire several times ...

codebehind, ViewModels? - ? ** ?

, ?

:

XAML

<sdk:Page Loaded="Page_Loaded" Unloaded="Page_Unloaded">

CODE BEHIND

private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        RegisterMessages();
    }

    private void Page_Unloaded(object sender, RoutedEventArgs e)
    {
        Messenger.Default.Unregister(this);
    }
+5
1

, . , , . Messenger.Default.Unregister, Unloaded, (, Loaded.

, , . MessageBox.Show(), , . ...

MVVM , Caliburn. , View- ViewModel. Caliburn 100% - , .

+4

All Articles