Supervision problems in WP7, I can’t determine if I need to restore or create instances / request new data

I just ran into a thunder issue / issue in WP7. let's say I have 3 pages, FirstPage.xaml, SecondPage.xaml and ThirdPage.xaml. natural flow will be:

FirstPage.xaml β†’ SecondPage.xaml β†’ ThirdPage.xaml

In other words,

main page β†’ page with a list of objects β†’ page with a detailed display of one object from the previous page

when I switch from FirstPage.xaml to SecondPage.xaml, I need to do a database query to get the List in SecondPage.xaml. Then I need to go to ThirdPage.xaml from SecondPage.xaml (after I select one MyObject from the list). at the moment, the tomb is becoming very confused for me.

I know when going FirstPage.xaml -> SecondPage.xaml, the constructor of SecondPage.xaml.cs is called. I know when navigating ThirdPage.xaml β†’ SecondPage.xaml (returning back by pressing the "Back" button or NavigationService.GoBack ()), the constructor of SecondPage.xaml.cs is NOT called. when I switch from SecondPage.xaml to ThirdPage.xaml, I store view-model objects (VM) in PhoneApplicationService.Current.State (SecondPage.xaml.cs.OnPageNavigatedFrom ()).

my (incorrect) strategy was, well, if the constructor of SecondPage.xaml.cs is called in one instance (FirstPage.xaml β†’ SecondPage.xaml), but not in another instance (ThirdPage.xaml β†’ SecondPage.xaml), then I can install in constructor a logical flag whether to make a new database query or restore the state of the page (from PhoneApplication.Current.State). the boolean flag is first set to false, but only true in the SecondPage.xaml.cs constructor.

I thought this worked fine, but then when I clicked the start button to leave the application, and then click the back button to return to the application, the SecondPage.xaml.cs constructor was called. therefore, I am making another new DB query instead of restoring a state that is NOT the intended behavior.

My question is how to find out when to make a new database query and restore when the user clicks on start and then back to go to the application? I thought about how to solve this myself, but most of what I came up with were kludges; it seemed indecent, and as if I was messing around to make it work. for example, I thought I could go to querystring from FirstPage.xaml to SecondPage.xaml (i.e. /SecondPage.xaml?freshDbQuery=1), but when I switch from ThirdPage.xaml back to SecondPage.xaml, this is the key querystring pair , freshDbQuery = 1, always like that! (since you can tell, I don't know wp7 very well).

any help is appreciated.

+1
source share
1 answer

All your processing for the tomb should be done in the OnNavigatingFrom ** and OnNavigatedTo .

You can create all the target 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 it preferably OnNavigatedFrom where possible.

+2
source

Source: https://habr.com/ru/post/1384346/


All Articles