简体   繁体   中英

NavigationContext.QueryString isn't cleared after tombstoning in WP8 Silverlight page app

Let's consider a two-page Silverlight WP application: the main page PageA, and another PageB we can open from PageA and pass a parameter into it. As Charles Petzold suggests in his bestseller 'Programming WP7', we can instantiate PageB using a statement like this:

NavigationService.Navigate(new Uri(
    "/EditEntryPage.xaml?ItemIndex=" + myItemIndex, UriKind.Relative));

And then use the following construct in the OnNavigatedTo/OnNaviagetdFrom events of PageB to process the parameter and the case when the app was tombstoned and reactivated again:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string myParam;
    if (this.NavigationContext.QueryString.TryGetValue("ItemIndex", out myParam))
    {
        fItemIndex = int.Parse(myParam);
    }
    else if (PhoneApplicationService.Current.State.ContainsKey(APP_STATE_KEY_ITEM_INDEX))
    {
        fItemIndex = (int)PhoneApplicationService.Current.State[APP_STATE_KEY_ITEM_INDEX];
    }
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    PhoneApplicationService.Current.State[APP_STATE_KEY_ITEM_INDEX] = fItemIndex;
}

However, we have one problem if the user left the app from PageB, the app was tombstoned, and the user returns again to the app to the same PageB using the task manager. In this case, NavigationContext.QueryString in the OnNavigatedTo event returns the same ItemIndex parameter as if the page were called from PageA and the second 'if' is never executed!

Did I miss something important (an app settings, etc), or the behavior was changed in WP8 and we can no longer use this approach?

The query string behavior has not changed from WP7 to WP8. If it is in the uri, it will stay there upon resuming from tombstone or fast app switching.

One method I use to tell the difference is with the NavigationEventArgs.IsNavigationInitiator property. It will be true only when navigating inside your app, and false when you are being resumed from the OS. So if you were to change your first if statment to the following then it may work as you expected:

if (e.IsNavigationInitiator 
      && this.NavigationContext.QueryString.TryGetValue("ItemIndex", out myParam))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM