As i'm using the Pivot
control in my app, I wonder how can I resume to the last selected pivot item after the user tombst开发者_高级运维oned the app (Launched the App, pressed the windows button and pressend the back button to resume)?
(I tried to add some code in the Application_Deactivated
and Application_Deactivated
but didn't work)
To save the state of the Pivot you should be using the State
property of the page in the OnNavigatedTo
and OnNavigatedFrom
methods.
Here is a basic example:-
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (State.ContainsKey("pivotIndex"))
myPivot.SelectedIndex = (int)State["pivotIndex"];
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
State["pivotIndex"] = myPivot.SelectedIndex;
}
Note that the Windows Phone will handle the persisting of this state in the case where your application gets tombstoned. This approach also enables your page to navigate to elsewhere in the app and on navigation back your pivot state is restored.
If you keep track of your pivot's SelectedIndex
, you can restore this value on return from tombstoning.
Here's a straight forward walkthrough on saving data when tombstoned.
Tombstoning on the Win7 Mobile Platform
To implement tombstoning you really need to add code to all of:
Save:
- Application_Deactivated
- Application_Closing
Load:
- Application_Launching
- Application_Activated
Then you also need to override "OnNavigatedTo" within the Pivot Page - this is the ideal time to set the SelectedIndex for your pivot.
精彩评论