UWP C# - Re-launch app from notification click

元气小坏坏 提交于 2019-12-24 06:06:01

问题


I'm writing a UWP app, and I have a ScheduledToastNotification that is added to the schedule when the app is suspended (e.g. like a reminder). However, if I close the app, the notification appears on time, but when I click on the notification (no buttons, just on the notification in general), the app doesn't launch correctly, stopping at the splash screen.

How do I get the app the re-launch correctly?

Thanks.


回答1:


You should override OnActivated in App.Xaml.cs and handle this like

protected override void OnActivated(IActivatedEventArgs args)
        {
            if (args.Kind == ActivationKind.ToastNotification)
            {
                var toastArgs = args as ToastNotificationActivatedEventArgs;
                var arguments = toastArgs.Argument;

                if (arguments == "ARG")
                {
                    Frame rootFrame = Window.Current.Content as Frame;
                    if (rootFrame == null)
                    {
                        rootFrame = new Frame();
                        Window.Current.Content = rootFrame;
                    }
                    rootFrame.Navigate(typeof(YOURPAGE));
                    Window.Current.Activate();
                }
            }
        }


来源:https://stackoverflow.com/questions/37636189/uwp-c-sharp-re-launch-app-from-notification-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!