Understanding Application Events

Applications that provide support for the application framework can be also managed with application events. This is the typical case of client applications such as Windows Forms and WPF applications. Windows Forms applications provide the following events:

NetworkAvailabilityChanged, which is raised when the network becomes available or the connection is no longer available

ShutDown, which is raised when the application shuts down

Startup, which is raised when the application starts

StartupNextInstance, which is raised when another instance of the application starts up

UnhandledException which is raised when the application encounters an unhandled exception during tasks that involve the application framework

Applications events are handled within the code file that implements My.Application customizations, which by default is Application.designer.vb. The following example shows how you can intercept the network state change:

image

In WPF applications you have more events that you can handle. First, application events in WPF are handled in the Application.xaml.vb file. You have the following events available:

Activated, which is raised when the application gets the foreground focus

Deactivated, which is raised when the application loses the foreground focus

DispatcherUnhandledException, which is raised when the Dispatcher object encounters an unhandled exception

Exit, which is raised when the application shuts down

LoadCompleted, Navigated, Navigating, NavigationFailed, NavigationProgress, and NavigationStopped, which are raised in case of navigation applications, which is self-explanatory if you think of navigation between pages

Startup, which is raised when the application starts up and specifies the main UI object

FragmentNavigation, which is raised when navigating to a specific XAML Uri

SessionEnding, which is raised when the user logs off from Windows or is shutting down the system

The following example shows how you can intercept the SessionEnding event and decide to back up your work if the e.ReasonSessionEnding property has value ReasonSessionEnding.Shutdown:

image

Application events provide a great way for getting information on what happens behind the scenes of the application lifetime.

Summary

The My namespace is a unique feature of Visual Basic language starting from VB 2005, offering lots of shortcuts to most common operations. In this chapter you saw how you can interact with your application with My.Application and how you can perform operations on your system with My.Computer, including file operations, Registry operations and clipboard operations. You then got information about an important feature provided by My.Settings, which has the capability to save and load user preferences using the Visual Studio Designer and managed code, which is a convenient way if compared to old initialization files. Next you saw about My.Resources, a special place for embedding files in your executable, typically for Console and Windows Forms applications. Finally, you got in touch with one of the most important features in My: the ability to extend the namespace with custom members, both at root level and existing members.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.244.154