kamil b
kamil b

Reputation: 43

Lifecycle of page

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?

Upvotes: 1

Views: 925

Answers (2)

VT Chiew
VT Chiew

Reputation: 703

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.

Upvotes: 0

Igor Ralic
Igor Ralic

Reputation: 15006

You can find some information about page lifecycle and events being called in this blog post

Page State - step by step

Upvotes: 1

Related Questions