Search code examples
uwpmvvmcross

How to respond to toast notifcations in MVVMCross UWP app


I have a windows UWP app written with MVVPCross. The application method OnActivated is called when the user clicks on a Toast Notifcation message.

protected override void OnActivated(IActivatedEventArgs e)
{
    // Handle toast activation
    if (e is ToastNotificationActivatedEventArgs)
    {
        var toastActivationArgs = e as ToastNotificationActivatedEventArgs;

    }
}

When I get this event it want to:

  • clear the navigation history
  • Renavigate to the home page with information passed to it from the toast event
  • The home page will then reload itself and set the initial selection based on info passed to it

I'm looking for guidance how I can force a navigation via the mvvmcross framework. I know how to navigate from one view model to another but in this case, I'm not in the context of a view or a view model - i'm in the context of the Windows.UI.Xaml.Application object.

thanks, michael


Solution

  • I think the only thing you need is instance of current frame

            if (args.Kind == ActivationKind.ToastNotification)
            {
                try
                {
                    if (args.PreviousExecutionState == ApplicationExecutionState.Running)
                    {
                        var fr = Window.Current.Content as Frame;
                        var toastActivationArgs = args as ToastNotificationActivatedEventArgs;
                        fr.Navigate(typeof(MainPage), toastActivationArgs);
                    }
                }
                catch { }
            }
    

    In your MainPage OnNavigatedTo action you can get Navigation parameters and do whatever you want .