Esempio n. 1
0
        private async void MainWindow_OnClosing(object sender, CancelEventArgs e)
        {
            if (CloseState.ForceClose.Equals(WindowCloseState))
            {
                return;
            }
            if (CloseState.Closing.Equals(WindowCloseState))
            {
                if (WindowState.Equals(WindowState.Minimized))
                {
                    WindowState = WindowState.Normal;
                }

                e.Cancel = true;
                SystemSounds.Exclamation.Play();
                return;
            }

            WindowCloseState = CloseState.Closing;

            if (Context.IsNotSaved)
            {
                e.Cancel = true;

                if (WindowState.Equals(WindowState.Minimized))
                {
                    WindowState = WindowState.Normal;
                    SystemSounds.Exclamation.Play();
                    RootDialog.Focus();
                }

                if (RootDialog.IsOpen)
                {
                    DialogHost.CloseDialogCommand.Execute(null, RootDialog);
                }

                var dialog = new DecisionDialog("Configuration has changed", "Do you want to save before closing?");
                var result = (MessageBoxResult?)await DialogHost.Show(dialog, "RootDialog");

                if (result == null)
                {
                    return;
                }

                switch (result)
                {
                case MessageBoxResult.None:
                case MessageBoxResult.Cancel:
                    WindowCloseState = CloseState.None;
                    return;

                case MessageBoxResult.OK:
                case MessageBoxResult.Yes:
                    Context.SaveContext();
                    WindowCloseState = CloseState.ForceClose;
                    Close();
                    break;

                case MessageBoxResult.No:
                    WindowCloseState = CloseState.ForceClose;
                    Close();
                    break;
                }
            }
        }