#197 – Override Application Class Methods for Standard Events
January 25, 2011 Leave a comment
We’ve already mentioned some of the main events that the Application class fires–e.g. Startup, Exit and SessionEnding. We also showed how you can add event handlers to your Application-derived class to handle these events, using SessionEnding as our example.
Rather than adding an event handler for Application.SessionEnding to our class, we could have just overridden the OnSessionEnding method. This is more appropriate in a class that already derives from Application. Instead of specifying the handler in App.xaml and adding the handler to App.xaml.cs, we can just add code for the override to App.xaml.cs.
As an example, the override for OnSessionEnding could look something like this:
public partial class App : Application { protected override void OnSessionEnding(SessionEndingCancelEventArgs e) { // Always call method in base class, so that the event gets raised. base.OnSessionEnding(e); // Place your own SessionEnding logic here } }