Lifecycle of page - windows-phone-7

I looking some information about lifecycle of pages, especially when the page constructor is called?
This happens when page start, even if it had been showed 5 sec earlier?
The Components of page are initialize every time when page shows and are destroyed when another page shows?

You can find some information about page lifecycle and events being called in this blog post
Page State - step by step

A page is instantiated when you navigate to it (assuming you are running on Silverlight). When you navigate away from it the state will be saved in a stack. If you navigate back (by calling the GoBack method on the NavigationService class) the page will be resumed from its state, without calling the constructor.
However, if you navigate away from an existing page, and re-navigate to the page by calling the Navigate method, a brand new instance of the page is instantiated, and thus the constructor will be called again.
So, to answer your question, the only sure-fire method that will be called when a page is shown is OnNavigatedTo (and OnNavigatedFrom when it's leaving, be it destroying on just navigating away). It is advisable to put initialization code and disposing code in the two methods, instead of relying on the constructor.

Related

Binding updates to shell component pages delayed

So I have a Shell that has a home page and a settings page.
The settings page makes changes to a singleton service that the home's ViewModel is binded to (MVVM).
I have set up break points on the PropertyChangedEvents of elements on the Home page and they hit as soon as changes are made to the service's properties and their PropertyChanged is executed.
However, upon going back to the home page via the Shell, the page's UI only updates when the page is active and visible. This causes fields to flash when they change. But the breakpoints suggest that they should have updated before.
Is there a way to force refresh the UI before or are there any other alternatives?
For now I have made my view perform a Fade In animation so that changes to the ViewModel don't cause flashing.
Even I have a Xamarin Forms Shell app, and I also had the same issue, So what you can do is, Make the common properties Global as in make those properties in App.xaml.cs and mark them as static.
So when the User changes the property on settings change the App.PropertyName.
Then on the OnApearing method of homepage call a method on the ViewModel say LoadDataValues() and read the App.PropertyName and modify the UI as needed.
What this will do is just before the HomePage appears it will keep the UI of HomePage ready.
Let me know if you have further issues.

The loaded event on MainPage is getting called twice on application startup

The loaded event on my MainPage is getting called twice on application startup.
The constructor of the page is only getting once, and I am pretty sure that I am only assigning the event handler once also. What is going on?
Took me a while to work out what was going on, but it turns out the loaded event shouldn't be used on pages for initialization tasks, as it is not guaranteed to be called only once.
OnNavigatedTo should always be used instead. See the MSDN article below.
Typically, you use the OnNavigatedTo method instead of creating an
event handler for the Loaded event. The OnNavigatedTo method is
preferable because it is only called once for each time the page
becomes active. The Silverlight framework raises the Loaded event each
time the element is added to the visual tree, which potentially can
happen more than once when activating a page.

constructor vs. onNavigatedTo

I am confused what I should do in constructor and what else should I do in a onNavigatedTo method in a Windows Phone page.
Particularly, where should I read storage and settings?
onNavigatedTo is performed all the time when you navigate to this page, i.e. after navigation back from another page or deactivation application.
Page constructor is performed only once BEFORE page is loaded (after tombstoning it also needs to be loaded again)

New Instance of the page on Navigation

I have a few pages in an Application that require A-Synchronous calls to be made for about 2-3 minutes to get Synchronized, the user may navigate away from that page during Synchronization and can come back again after visiting multiple pages and the sync continues all the time he is on other pages as well, when I go to a page from sync-page and press the Back button everything works fine.. but when i go to a page and navigate back to sync-page from Application Bar a new Instance of the Page is created and the Sync is just like Re-started.
Now i know every thing is working fine since new instance of a page is created when i call NavigationService.Navigate() , but what should i do in this scenario ? How to get the old instance of a page if it is there ?
Thanks...
You can't get an "old" instance of a page and it's not guaranteed that a backwards navigation will reload the previous instance of the page, it may be a new instance of the same page, but restored to the same state (assuming you saved any).
If you are trying to provide backwards navigation from the application bar then a) you probably shouldn't because that's what the back button is for, and b) you should make sure you use NavigationService.GoBack() instead of NavigationService.Navigate() because Navigate will always launch a new instance of your page.
If the page you want to get to is not the previous page, then it sounds like you are trying to implement non-linear navigation for which there is a recipe on the App Hub.
By the sounds of your scenario, you should handle this long running process separately (away from the view) and then display it's progress or results in a view when the user navigates to the relevant page.

Can I access the main page from an Application event?

I want to access a property of the main page of my application from inside the Application_Launching event. Is this safe to do? Does the page(-object) already exist at this point?
The application lifecycle model goes something like this:
App class constructor.
App.InitializePhoneApplication method (this is where the PhoneApplicationFrame that hosts page content is created).
App.Application_Launching.
Page constructor for the startup page.
App.CompleteInitializePhoneApplication (this is the first time outside of the Page class that you can access the page (from RootFrame.Content, though it's visual tree will not yet be created).
Page Loaded event (at this point the visual tree for the page has been loaded).
What is it that you are trying to achieve? You could set a value in isolated storage and read it in the page, or expose the value as a property in your App class.

Resources