问题描述
我的 Toast 通知有一个奇怪的问题(或者可能是我误解的预期行为).我想要的只是让 Toast 导航回用户点击开始"按钮时所在的页面.
I'm having a weird issue (or maybe it is expected behavior that I am misunderstanding) with my Toast notification. All I want is to have the toast navigate back to the same page that the user was on when they hit the Start button.
场景:
用户打开应用,显示为 Page1.xaml.用户点击按钮导航到 Page2.xaml.从这里,他们点击了开始按钮.这是我在 Page2.xaml.cs 上设置的 Toast 通知:
User opens app, they are shown Page1.xaml. User taps button to navigate to Page2.xaml. From here, they hit the Start button. Here is the Toast notification I set up on Page2.xaml.cs:
Microsoft.Phone.Shell.ShellToast toast = new Microsoft.Phone.Shell.ShellToast();
toast.Content = "App is still running!";
toast.Title = AppResources.ApplicationTitle;
toast.NavigationUri = new Uri("/Pages/Page2.xaml?fromToast=true", UriKind.Relative);
toast.Show();
我在查询字符串上传递 fromToast
以便我可以正确处理恢复".但是,当我点击显示的 Toast 通知时,我的 OnNavigatedTo
事件在 Page2.xaml 上触发两次.
I'm passing fromToast
on the querystring so that I can handle "resuming" correctly. However, when I tap the Toast notification that shows up, my OnNavigatedTo
event fires twice on Page2.xaml.
第一次 OnNavigatedTo
在点击 Toast 后在 Page2.xaml 上触发,没有查询字符串参数,但紧接着,它再次用我的 fromToast
参数触发.
The first time OnNavigatedTo
fires on Page2.xaml after tapping the Toast, there are no query string parameters, but then right after, it fires again with my fromToast
param.
为什么会触发两次?这是预期的吗?如果我从我的 toast
中删除 NavigationUri
属性,点击 Toast 只会将应用程序带回 Page1.xaml,这不是我想要的.
Why is it firing twice? Is this expected? If I remove the NavigationUri
property from my toast
, tapping the Toast only takes the app back to Page1.xaml which is not what I want.
有人见过这个吗?
推荐答案
这是启用 Fast Resume
时的正常行为.您必须了解它是如何工作的,才能决定如何处理用户体验.来自 MSDN 站点:
This is the normal behavior when Fast Resume
is enabled. You have to understand how it works to decide how you want to handle user experience. From the MSDN site:
通过快速恢复,当应用恢复时,系统会为启动点的目标创建一个新的页面实例,并将该页面放置在应用现有后台堆栈的顶部.
所以基本上点击 toast 将导致创建一个新的 Page2 实例,因此 NavigatedTo
被调用而没有查询字符串.由于您的 toast 具有深层链接,因此将请求另一个导航,从而创建另一个 Page2 实例,并使用 fromToast=true
查询字符串调用 NavigatedTo
.
So basically tapping on the toast will cause a new instance of Page2 be created and hence NavigatedTo
being called with no query strings. Since your toast has a deep link, another navigation will be requested causing another instance of Page2 be created and the NavigatedTo
called with fromToast=true
query string.
您可以通过检查 e.NavigationMode
属性来确定导航模式.第一次应该是 NavigationMode.Reset
然后第二次是 NavigationMode.New
.
You can determine the navigation mode by inspecting e.NavigationMode
property. It should be NavigationMode.Reset
the first time then NavigationMode.New
on the second time.
另请注意,默认情况下,从 Toast 启动应用会清除后台堆栈,因此您会将用户置于无限循环的 Toast 中.
Also be aware that by default launching an app from a toast will clear the backstack so you'll put your users into an infinite loop of toasts.
关于恢复工作的速度以及如何处理不同场景的好读物在这里:适用于 Windows Phone 8 的快速应用恢复
A good read on how fast resume works and how to handle different scenarios is here: Fast app resume for Windows Phone 8
我的建议是,您不需要告诉用户该应用仍在运行.这是 WP 中应用的默认行为,大多数用户已经知道这一点.
My advice is that you don't really need to tell the user the app is still running. This is the default behavior of apps in WP and most users already know that.
这篇关于WP8 OnNavigatedTo 在点击 Toast 通知后触发两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!