I have a windows phone 7 application how do I make sure it supports fast task switching?
What do I need as a developer to be sure my apps support fast task switching?
Ideally, I'm looking for like a developer check list of dos and don'ts.
I did search, but everything I found made me feel like I was missing something, tended t开发者_如何学JAVAo be more marketing instead of developer steps and technical details.
Thanks!
Most of the work for FAS is handled for you automatically. The main thing to keep in mind is what Tombstoning means to your application. When resuming via FAS, the intent is that you don't have to un-tombstone anything, so there's usually no need to restore view model state, or anything like that. There are a couple of places for which you will need to write code - here's a quick checklist.
PhoneApplicationPage.OnNavigatingFrom - Experiment with the controls you're using to make sure that FAS restores the data that was there for you. For example - the TextBox control correctly remembers everything you had put into it, but the MediaElement doesn't remember the video or where it's playback head was positioned.
PhoneApplicationPage.OnNavigatedTo - Anything that you saved off in OnNavigatingFrom needs to be re-applied here in OnNavigatedTo. For example - reloading the video source into the MediaElement, repositioning the video and starting it back up again.
Application.Activated - The event args for this event now contain a property called IsApplicationInstancePreserved. This property returns TRUE when the application is coming back through FAS, or FALSE when the application is coming back from Tombstoning. So you'd have code something like this:
private void Application_Activated(object sender, ActivatedEventArgs e)
{
if (!e.IsApplicationInstancePreserved)
{
RestoreStateFromTombstone();
}
}
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
SaveStateForTombstone();
}
That's the essentials. I haven't done any real stress testing yet on the FAS infrastructure to see where it breaks, but this has served me well for the experimenting I've done so far.
For more information, there's a short video from the MIX11 conference called Get Ready for Fast Application Switching presented by Adina Trufinescu that gives more details on FAS which definitely helped me get started.
/chris
If you're upgrading from a normal tombstoning application in Windows Phone 7.0, the only thing you will need to change is the check for e.IsApplicationInstancePreserved in the Application_Activated event - if this property is set to true, it means that you don't have to rehydrate from IsolatedStorage / State. As mentioned in Chris Koenig's excellent answer:
//Windows Phone 7.0
private void Application_Activated(object sender, ActivatedEventArgs e)
{
RestoreStateFromTombstone();
}
//Windows Phone 7.1/7.5/Mango
private void Application_Activated(object sender, ActivatedEventArgs e)
{
if (!e.IsApplicationInstancePreserved)
{
RestoreStateFromTombstone();
}
}
Symptoms for this are your databindings no longer working after you switch apps via multitasking (since you are recreating your object states so the databinding is no longer valid)
精彩评论