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?