This section describes some functions of the Application class. First, let's take a look at how to use the Application class to load a window:
Create a console program, introduce the related dll, and modify the Main () method.
app = win =
In this example, two methods are used to load the window through the Application class. This window is displayed as the main window. When the code executes the Run () method, the Application. StartUp event is triggered and the main window is displayed after the event is executed. Regardless of the method above, the application runs until the main window and all other windows are closed, the Run () method returns, and runs Run () before the application ends () code after the method.
Next we will introduce the general WPF application, and mainly use this method.
When using Visual Studio to create a WPF Application, we can use the WPF Application template provided by Visual Studio. It will create an App. xaml and App. xaml. cs file for us. Let's check the App. A StartupUri = "MainWindow. xaml ", which means to specify the main window of the program; in the file App automatically generated by the program. g. i. this value is used in cs, which is a URI format value. We can view the hidden App. xaml. cs file and find that there is only one App category inherited from System. Windows. Application.
Initially, the App class did not contain any code. However, we can add code to process Application events. Next, let's take a look at several important events in the Application class:
Startup:After the Application. Run () method, it is displayed before the main form. You can also use the StartupEventArgs. Args attribute of this event to obtain the Assembly command line parameters. You can even create and display the main window in the event, instead of StartupUri in App. XAML.
Exit:This event cannot be undone (for whatever reason, the Shutdown method is called, the Windows user logs out, or the end session is closed) before the application is closed ).
SessionEnding:Occurs when the user logs out or closes the Windows session in the operating system. You can check the SessionEndingCancelEventArgs. ReasonSessionEnding attribute value to identify the cause. To prevent the application from being closed, set SessionEndingCancelEventArgs. Cancel to true (false by default ).
Activated:When a window of the application is activated (including when the main window is started; users of the application are switched by using ALT + TAB or task manager; the user clicks a taskbar button in the application window.
Deactivated:This occurs when a window of the application is deactivated.
DispatcherUnhandledException:At any location in the application, this event occurs as long as an unhandled exception occurs. We can even use the DispatcherUnhandledExceptionEventArgs. Handled attribute in this event and set it to true (false by default) to allow the program to continue running.
There are 2 options used to process events: Associate Event Handlers through App. xaml, or rewrite the corresponding protected method directly.
I can also change the method of closing an application through ShutdownMode. There are three methods:
OnLastWindowClose:When the application is closed, when the last window is closed, or when the Shutdown () method is called. This is the default action.
OnMainWindowClose:The application is closed, the main window is closed, or the Shutdown () method is called.
OnExplicitShutdown:The application is closed only when the Shutdown () method is called. In this mode, if the Shutdown () method is not called, the application will never end (even if all windows have been closed ).
We have some knowledge about the WPF Application class. Next let's look at some other situations:
Access the Current Application object: Through the static Application. Current attribute, you can obtain the Current Application instance anywhere in the Application.
app = main = (MainWindow)app.MainWindow; ( window
In this example, note that the value of Application. Current. Windows is. When the window (including the main window) is displayed, it is added to the Windows collection. When the window is closed, it is removed from the Windows collection.
Single-instance applications:
Generally, you can load any number of copies of a WPF application if you want. In some cases, this is very reasonable. However, in other cases, this may be a problem. Let's take a look at an article and then look at a specific example:
1. First create a WPF application, and then delete the App. xaml file and its post-code file.
2. Add Microsoft. VisualBasic. dll file reference to the project
3. Create a class file named App and make the App class inherit System. Windows. Application
main =
= = ;
Main( single =
In this way, a simple application that can only run a single instance is successfully created. Next, we will build a document-based application based on the above Code.
6. Create a Document Reading window named Document. xaml to display the Document Reading content:
===
DocumentReference(Document document, == Document Document { ; Name { ;
Edit the Properties of Solution and modify the Command line arguments of the Debug tab. If multiple parameters require spaces.
In this way, the document will be automatically opened when we run the program. Note that all the. testDoc files in the project Copy to Output Directory are modified to Copy always.
Code File