The Sessionending event of the application class is the response when the computer shuts down or restarts (session end event),
So just add sessionending to the App.xaml.
<x:class= "Drivereasywpf.app" xmlns= "http// Schemas.microsoft.com/winfx/2006/xaml/presentation " xmlns:x="/http/ Schemas.microsoft.com/winfx/2006/xaml " StartupUri=" MainWindow.xaml " Startup= "Application_Startup" sessionending= "Application_sessionending" >
View Code
And this is done in the background code:
private void Application_sessionending (object sender, SessionEndingCancelEventArgs e) { //your code }
View Code
But Windows has no sessionending event, so if you want to listen to a computer shutdown or restart in a window,
Because the application class is used throughout the WPF project, in the background code of the other windows, such calls can be:
Public MainWindow () { InitializeComponent (); App.Current.SessionEnding + = current_sessionending; } private void Current_sessionending (object sender, SessionEndingCancelEventArgs e) { throw new NotImplementedException (); }
View Code
Through the App. Current. sessionending + = current_sessionending;
WPF implementations perform certain logic when a computer restarts or shuts down