2

I have a wpf project which has couple of windows, whne i navigate from one window to other i just hide one window and create instance of other if not created already, otherwise just make it visible

in all windows there is window closing event handler

 private void Window_Closing_1(object sender, CancelEventArgs e)
    {
         {

        string message = "You are trying to close window " + this.Name + " .Are you sure?";
        string caption = "Exit";
        MessageBoxButton buttons = MessageBoxButton.YesNo;
        MessageBoxImage icon = MessageBoxImage.Question;
        if (MessageBox.Show(message, caption, buttons, icon) == MessageBoxResult.Yes)
        {
            App.Current.Shutdown();

        }
        else
        {
         MessageBox.Show("Closing aborted");

        }
    }

but problem is this event is there in every window so when i close it lots of other prompt from other window which are invisible also show up. Is there a way to avoid other pronpts if i close in only one window?

2 Answers 2

3

Have you tried in App.xaml to add Exit attibute?

it will be a better solution since you will only have to write this confirmation exit code just once. about window name you could cast sender to window in order to find out its name

2
  • what is that ? I am sorry i am new to WPF. Do i need to write closing window logic in private void Application_Exit_1(object sender, ExitEventArgs e) in app.xaml ? Jan 12, 2013 at 8:01
  • window name is not important Jan 12, 2013 at 8:20
1

just create a static method in another class to handle closing of forms.

public class X 
{
    private static bool isClosingHandled = false;

    public static void HandleClose()
    {
        //// app closing logic
        isClosingHandled = true;
    }
}

private void Window_Closing_1(object sender, CancelEventArgs e)
{
    X.HandleClose();   
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.