When you create a Silverlight application the App.xaml.cs code behind file has the Application event handlers added for you

public App()
       {
           this.Startup += this.Application_Startup;
           this.Exit += this.Application_Exit;
           this.UnhandledException += this.Application_UnhandledException;
 
           InitializeComponent();
       }

In this post I will discuss about the Application.Exit and will use another for Application.Startup.

Application.Exit

Application.exit event helps the  developer to detect the shut down of a Silverlight application. This helps to perform common application closing tasks such as saving data for the next session.

Let's see an example where the application will store a value in a cookie when user exits.

Open App.xaml.cs and add the code you want to be execute just before the application is shut down

 private void Application_Exit(object sender, EventArgs e)
        {
            Cookie.SetCookie("level","5");
        }
 
Wherein, I have wrapped the Cookie functions of Tip of the day #18 in a class.
 
Now the cookie value can be used when user loads the application again.
 
public Page()
        {
            InitializeComponent();
            level.Text = Cookie.GetCookie("level");
        }