Application class is the most important class responsible for managing the application level functionality.The life cycle of WPF application is managed by the Application class.
It manages functionality such as setting which window is currently active in the application,handling unhanded exceptions and defining resources to be shared across the application.
A running WPF application consists of a single object of Application class.It raises different events when the WPF application is executing.
- Run method starts the application
- After the Run method is called it fires the StartUp event.This indicates that the application is starting.
- Activated is raised when the application starts executing
- Deactivated is raised when the application goes to background.
You can implement the following methods in your application class to handle the above events:
- OnStartup(StartupEventArgs)
- OnActivated(EventArgs)
- OnDeactivated(EventArgs)
- OnExit(ExitEventArgs)
You implement these methods in the code behind of your app.xaml file
public partial class App : Application { private void OnStartup(StartupEventArgs)(object sender, StartupEventArgs e) { // } private void OnActivated(object sender, StartupEventArgs e) { // } }
It also provides the Run() method which starts the application
app.Run();
You define application resources using the XAML of the application class as:
<Application x:Class="Application" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> </Application.Resources> </Application>
The resources which you define in the XAML of the application class can be accessed across the entire application.
Leave a Reply