WPF closes the application method, while wpf closes the application
Many people think that closing an Application should be very simple, for example, an Application in Windows form. exit (); method can solve the problem, but do not abuse it in WPF, because the Application class in WPF does not have this method, but there is an Exit event driver, closing a program in a WPF application requires a lot of attention:
When a WPF application is closed, the ShutdownMode attribute is set and the value of the enumerated type in 3 is:
1) Close the application when the last form of the OnLastWindowClose application is closed.
2) Close the application when the main form of the OnMainWindowClose application is closed.
3) OnExplicitShutdown: indicates that the call is closed.
In OnExplicitShutdown mode, the ShutDown method of the Application instance must be displayed.
Example: Application. Current. Shutdown (-1); here Application. Current returns the Current Application instance of the Current Application.
Note that the above is not applicable to XBAP, and XBAP is automatically disabled when the browser is closed.
Requirement: the entire program (WPF) needs to be closed in many subforms)
Winform implementation: Application. Exit ();
WPF implementation:
App. xaml file:
Code <Application x: Class = "pc. App"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri = "Windows1.xaml" ShutdownMode = "OnExplicitShutdown">
Windows1.xaml file (Part ):
<Button Margin = "37,0, 15,15" Style = "{DynamicResource btn_Exit}" Content = "Button" Grid. column = "2" Grid. row = "3" Height = "41" verticalignment = "Bottom" Width = "100" x: Name = "btn_Exits" Click = "btn_Exits_Click"/>
Windows1.xaml. cs file (Part ):
Private void btn_Exits_Click (object sender, RoutedEventArgs e)
{
Application. Current. Shutdown ();
}
Very simple! After that, you only need to add Application. Current. Shutdown () to the button event to exit the program.
In addition, there is a way to forcibly terminate the application process
Environment. Exit (0) can immediately interrupt program execution and Exit
This method is similar to finding and terminating a process in the task manager, that is, closing the process immediately, no matter whether the process is currently working or not.
In WPF, this method is called to exit immediately without waiting for the current job to complete.