简体   繁体   中英

How to show Notification Message on Shutdown of Application WPF

I made application in which when user will click on close button of MainWindow whole application will shutdown. I want to show a Notification after closing of application. How to show a toast message as application shuts'down? Here Code is :

private void Close(object sender, EventArgs e)
{
     base.OnClosed(e);
     Application.Current.Shutdown();
 }

Can any one answer my question? Feel Free to ask if my question is not clear!

Try implementing a handler for the Window.Closing event:

private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    MessageBox.Show("Hi, I'm closing!");
}

This will occur before the Window.Closed event.

UPDATE >>>

@Andy and Tameen, please take a look at the Window.Closing Event page at MSDN to see when this event really occurs.

Occurs directly after Close is called, and can be handled to cancel window closure.

UPDATE 2 >>>

Your question does not state that you want to cancel the Close event. However, that is exactly what the Closing event is for:

private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    MessageBox.Show("Hi, I'm closing!");
    e.Cancel = true;
}

Your application shutdowns by default when the mainwindow closes. Allow the button to signal the window close and handle the toast in the Closed event.

<Window ... Closed="Window_Closed" />

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.Close();
}

private void Window_Closed(object sender, EventArgs e)
{
   MessageBox.Show("Cya");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM