MVVM Light - WP7 / Applications Page

Is there a way to use MVVM Light to handle application events like Closed, Deactivated, Activated, etc.

+3
source share
2 answers

One thing you can do is handle these events in App.xaml.cs and send them a message using the default Messenger instance. Then you just need to register any model models to receive the message. If you need to cancel the event, use a callback message telling the application to cancel.

+4
source

Thanks to Matt Casto for sending me in the right direction.

Here is the working code:

App.xaml.cs:

    private void Application_Activated(object sender, ActivatedEventArgs e)
    {
        Messenger.Default.Send(new NotificationMessage<AppEvents>(AppEvents.Activated, string.Empty));
    }
    private void Application_Deactivated(object sender, DeactivatedEventArgs e)
    {
        Messenger.Default.Send(new NotificationMessage<AppEvents>(AppEvents.Deactivated, string.Empty));
    }
    private void Application_Closing(object sender, ClosingEventArgs e)
    {
        Messenger.Default.Send(new NotificationMessage<AppEvents>(AppEvents.Closing, string.Empty));
    }

ViewModel:

Messenger.Default.Register<NotificationMessage<AppEvents>>(this, n =>
{
    switch (n.Content)
    {
        case AppEvents.Deactivated:
            _sessionPersister.Persist(this);
            break;
        case AppEvents.Activated:
            var model = _sessionPersister.Get<TrackViewModel>();                
            break;
    }
});
+5
source

Source: https://habr.com/ru/post/1756030/


All Articles