The loaded event on MainPage is getting called twice on application startup - windows-phone-7

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.

Related

OnBackKeyPress in windows phone only works in staring page

I need handle hardware back button in every pages but it is not working.
I can only invoke OnBackKeyPress in main page. and other pages run same functionality that I put in main page OnBackKeyPress event.
I googled it more than 2 hours but didn't find any solution.
It depends on exactly what you are doing. There are two ways to capture the Back key: override OnBackKeyPress of the PhoneApplicationPage class, or attach an event listener to RootFrame.BackKeyPress. If you are attaching an event listener then even if you leave the current page the event listener will still be attached and will be called whenever the Back key is pressed. You'll need to be more specific with exactly what you are doing, what you expect to see, and what you are seeing.

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?
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.

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)

advantages to put code in app.xaml.cs in wp7

I am wondering if there are any advantages to putting the code into app.xaml.cs (Application_Launching) compared to the code I've put into the mainpage.xaml.cs (MainPage()) section.
The key thing to bear in mind is that the Application object in App.xaml.cs is where you get notification of the application lifecycle events.
See Execution Model for Windows Phone on MSDN for more information about this.
By using the Launching/Activated methods on your Application object, you can make sure you are initialising your whole app correctly when it starts or is resumed after tombstoning (or returns from the dormant state in WP7 Mango).
Code in your startup page (MainPage.xaml.cs) is used to initialize that one application page when it is navigated to. This is typically done in the OnNavigatedTo method as you can't rely on pages being freshly constructed every time you navigate to them.
Of course you will also need to handle the other events for application exit and deactivation, and navigation away from each of your pages.
Also bear in mind that if you are targeting WP7 Mango, you can start the app from a deep link in an alarm, reminder or toast to a page other than your normal startup page. If the application starts like this, code in your startup page MainPage.xaml.cs may not run, but code in your Application object always would.

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