0

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!

Tameen Malik
  • 1,258
  • 6
  • 17
  • 45

2 Answers2

3

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;
}
ChrisF
  • 134,786
  • 31
  • 255
  • 325
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Removed my downvote since the incorrect statement has been removed. Note that when I originally downvoated this text was included "This will occur `after` the Window.Close `event`." Technically there is no Close event, only Closing and Closed. Note that `close event` and `close method` are very different things, which was why I downvoted, since I assumed the text meant `Closed event`, not `Close method`. – Andy Sep 10 '13 at 12:22
1

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");
}
jamesSampica
  • 12,230
  • 3
  • 63
  • 85