Андрей Про
Андрей Про

Reputation: 799

App Current Shutdown closing window prompting

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?

Upvotes: 2

Views: 362

Answers (2)

iltzortz
iltzortz

Reputation: 2412

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

Upvotes: 3

daryal
daryal

Reputation: 14919

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();   
}

Upvotes: 1

Related Questions