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 t开发者_JS百科he 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.
精彩评论