Many people think that disabling the applicationProgramIt 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" Verticalalignment = "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.