Silverlight Application class provides a number of features to developers and Application Lifetime management is one feature that is being offered by Silverlight Application class. You can add code to your application class that runs at the following points in the application lifetime:

The application class constructor

Application class constructor can be used for basic initialization of variables and attaching event handlers. Application class constructor typically includes a call to InitializeComponent method which is responsible for merging of XAML and code behind file. Generally your application classes are defined using XAML markup and a code-behind file.

public Sub New()
End Sub

The application Startup event

Silverlight plug-in raises Application Startup event after loading application package. At this point of time all application package assemblies are loaded and available for use. Application Startup event can be used for

  • Process data that you retrieve at startup, such as initialization and URL parameters, or data stored in a previous application session.
  • Display the application user interface (UI).
  • Begin asynchronous downloads of additional resource files or assemblies.
Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
    Me.RootVisual = New MainPage()
End Sub

Application Exit

The Application.Exit event occurs when one of the following actions takes place:

  • The user closes the Web page hosting the Silverlight plug-in.
  • The user refreshes the host Web page.
  • The user navigates the browser away from the host Web page.
  • The host Web page uses JavaScript and the HTML DOM to remove the plug-in from the page.
  • The user logs off or shuts down the operating system.

One common use for the Application.Exit event is to save application settings by using the IsolatedStorageSettings class. For more information, see How to: Store and Retrieve Application Settings Using Isolated Storage.

Private Sub Application_Exit(ByVal o As Object, ByVal e As EventArgs) Handles Me.Exit

End Sub