Michael Foster
Michael Foster

Reputation: 125

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:

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

Upvotes: 0

Views: 153

Answers (2)

iam.Carrot
iam.Carrot

Reputation: 5276

I don't have much experience with MVVMCross but a quick google search brought me up to speed. So here is the thing:

  1. To clear the navigation back stack (navigation history) refer this blog. It defines a clean way to do so. Also this answer on stack does provide insights to a more application point of view.
  2. To navigate from to the home page you'll need to get the current instance of the Frame which is actually hosting your app. so a quick Frame rootFrame = Window.Current.Content as Frame; should get you done. More about this in the documentation here. Do a quick search for "Current" on the web site and you'll find the code
  3. To refresh the page, you can refer This question on stack

Upvotes: 1

Ali NGame
Ali NGame

Reputation: 464

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 .

Upvotes: 1

Related Questions