开发者

problems with tombstoning in WP7, cannot tell if i need to restore or instantiate/query new data

开发者 https://www.devze.com 2023-03-19 17:42 出处:网络
i\'ve just run into the infamous tombstoning problem/issue in WP7. let\'s say i have 3 pages, FirstPage.xaml, SecondPage.xaml, and ThirdPage.xaml. the natural flow will be:

i've just run into the infamous tombstoning problem/issue in WP7. let's say i have 3 pages, FirstPage.xaml, SecondPage.xaml, and ThirdPage.xaml. the natural flow will be:

FirstPage.xaml -> SecondPage.xaml -> ThirdPage.xaml

in other words,

main page -> page with list of objects -> page displaying one object in detail from previous page

when i go from FirstPage.xaml to SecondPage.xaml, i have to do a database query to get a List in SecondPage.xaml. i then need to go to a ThirdPage.xaml from SecondPage.xaml (after i select one MyObject from List). at this point, tombstoning is becoming very confusing for me.

i know when going FirstPage.xaml -> SecondPage.xaml, the constructor of SecondPage.xaml.cs is called. i know when going ThirdPage.xaml -> SecondPage.xaml (going back, by hitting the back button or NavigationService.GoBack()), the constructor of SecondPage.xaml.cs is NOT called. when i move from SecondPage.xaml to ThirdPage.xaml, i store the view-model (VM) objects in PhoneApplicationService.Current.State (SecondPage.xaml.cs.OnPageNavigatedFrom()).

my (flawed) strategy was, well, if the constructor of SecondPage.xaml.cs is called in one instance (FirstPage.xaml -> SecondPage.xaml), but not in the other instance (ThirdPage.xaml -> SecondPage.xaml), then i can set a boolean flag in the constructor whether to do a fresh DB query or to restore the page's state (from PhoneApplication.Current.State). the boolean flag is set to false initially, and only set to true in the constructor of SecondPage.xaml.cs.

i thought this worked well, but then when i pressed the start button to leave the app and then hit the back button to come back to the app, the constructor of SecondPage.xaml.cs was called. so i do another fresh DB query instead of restoring the state, which is NOT the intended behavior.

my question is this, how do i know when to do a fresh DB query vs a restore when the user hits start and then back to get to the app? i thought about how to solve this myself, but most of what i thought up were kludges; it seemed un-natural and as if i was tinkering to get things to work. for example, i thought i can pass in a quer开发者_运维问答ystring from FirstPage.xaml to SecondPage.xaml (i.e. /SecondPage.xaml?freshDbQuery=1), but when i move from ThirdPage.xaml back to SecondPage.xaml, that querystring key-value pair, freshDbQuery=1, is always so! (so as you can tell, i don't know wp7 too well).

any help is appreciated.


All your handling for tombstoning should be done in the OnNavigatingFrom** and the OnNavigatedTo events.

You can create all purpose handlers for your situation with the following:

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
    {
        this.State.Clear();
        this.State.Add("db_data", ***Serialized version of the DB returned data***);
    }

    base.OnNavigatingFrom(e);
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (this.State.ContainsKey("db_data"))
    {
        this.SomethingOnPage = DeserializeToAppropriateType(this.State["db_data"]);
    }

    base.OnNavigatedTo(e);
}

** Use this in preference to OnNavigatedFrom wherever possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消