In this post I will start from where I left in the last post. So, I will be talking about various application level events in a Windows Phone Application. These events are pretty similar to events in Global.asax.cs in case of ASP.net and Application Object in case of Windows Application. These events are registered in “App.xaml”. If you open **“App.xaml” **in visual studio you will see following XAML.

<application.resources></application.resources><application.applicationlifetimeobjects></application.applicationlifetimeobjects>

Out of the above XAML if you look you will see following 4 events of “PhoneApplicationService” class

  1. Launching (event handler function is “Application_Launching”)
  2. Closing (event handler function is “Application_Closing”)
  3. Activated (event handler function is “Application_Activated”)
  4. DeActivated (event handler function is “Application_Deactivated”)

All of the above mentioned event handler functions are defined in ( you guessed it, right :) ) “App.xaml.cs”. if you open **“App.xaml.cs” **and look at the comments on these functions, there usage is explained to an extent.

// Code to execute when the application is launching (eg, from Start) // This code will not execute when the application is reactivated private void Application_Launching(object sender, LaunchingEventArgs e) { } // Code to execute when the application is activated (brought to foreground) // This code will not execute when the application is first launched private void Application_Activated(object sender, ActivatedEventArgs e) { } // Code to execute when the application is deactivated (sent to background) // This code will not execute when the application is closing private void Application_Deactivated(object sender, DeactivatedEventArgs e) { } // Code to execute when the application is closing (eg, user hit Back) // This code will not execute when the application is deactivated private void Application_Closing(object sender, ClosingEventArgs e) { }

Before, going into details of these events. One should know a few basic details about application life cycle of Windows Phone 7.

  1. First is that an application closes when users clicks the back button, that looks like following icon in windows phone simulator. WP7Back
  2. Second is that an application goes to sleep mode or in Windows Phone Terminology application gets tombstoned, when users press start button, that looks like following icon in windows phone simulator. Application can get tombstoned, because of various events. for example, device got locked, an incoming phone call etc.In this mode application’s process is killed, but the state of application is still maintained by the Windows Phone Operating System.WP7Home

All the above mentioned four events are related to these 2 basic states(closed and tombstoned) of an application. and it is pretty evident that the “Application_Launching” and “Application_Closing”are the events that are raised when application starts(“Application_Launching”) and when application closes(“Application_Closing”). The other two events are related to application getting out of tombstone mode(“Application_Activated”)and getting tombstoned(“Application_Deactivated”).

Next question that comes into mind as developer is what operations am I suppose to perform in these events. That I will cover in my next post Smile. See ya..