Ejemplo n.º 1
0
        public Task <SwitchProfileResult> WaitForButtonPressAsync()
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                this.Focus();
                PART_ProfileComboBox.Focus();
            }));

            var tcs = new TaskCompletionSource <SwitchProfileResult>();

            RoutedEventHandler negativeHandler    = null;
            KeyEventHandler    negativeKeyHandler = null;

            RoutedEventHandler affirmativeHandler    = null;
            KeyEventHandler    affirmativeKeyHandler = null;

            RoutedEventHandler importHandler     = null;
            RoutedEventHandler newProfileHandler = null;

            KeyEventHandler escapeKeyHandler = null;

            Action cleanUpHandlers = () =>
            {
                this.KeyDown -= escapeKeyHandler;

                PART_NegativeButton.Click    -= negativeHandler;
                PART_AffirmativeButton.Click -= affirmativeHandler;
                PART_NewProfileButton.Click  -= newProfileHandler;

                PART_NegativeButton.KeyDown    -= negativeKeyHandler;
                PART_AffirmativeButton.KeyDown -= affirmativeKeyHandler;
            };

            escapeKeyHandler = (sender, e) =>
            {
                if (e.Key == Key.Escape)
                {
                    cleanUpHandlers();

                    tcs.TrySetResult(null);
                }
            };

            negativeKeyHandler = (sender, e) =>
            {
                if (e.Key == Key.Enter)
                {
                    cleanUpHandlers();

                    tcs.TrySetResult(null);
                }
            };

            affirmativeKeyHandler = (sender, e) =>
            {
                if (e.Key == Key.Enter)
                {
                    cleanUpHandlers();

                    tcs.TrySetResult(new SwitchProfileResult()
                    {
                        Input = this.Input, Result = SwitchProfileDialogResult.OK
                    });
                }
            };

            negativeHandler = (sender, e) =>
            {
                cleanUpHandlers();

                tcs.TrySetResult(null);

                e.Handled = true;
            };

            affirmativeHandler = (sender, e) =>
            {
                cleanUpHandlers();

                tcs.TrySetResult(new SwitchProfileResult()
                {
                    Input = this.Input, Result = SwitchProfileDialogResult.OK
                });

                e.Handled = true;
            };

            importHandler = (sender, e) =>
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.InitialDirectory = Environment.CurrentDirectory;
                dialog.Multiselect      = false;
                if (dialog.ShowDialog() != true)
                {
                    return;
                }

                string fileName = dialog.FileName;
                if (string.IsNullOrEmpty(fileName))
                {
                    return;
                }

                cleanUpHandlers();

                tcs.TrySetResult(new SwitchProfileResult()
                {
                    Input = fileName, Result = SwitchProfileDialogResult.Import
                });

                e.Handled = true;
            };

            newProfileHandler = (sender, e) =>
            {
                cleanUpHandlers();

                tcs.TrySetResult(new SwitchProfileResult()
                {
                    Input = string.Empty, Result = SwitchProfileDialogResult.New
                });

                e.Handled = true;
            };

            PART_NegativeButton.KeyDown    += negativeKeyHandler;
            PART_AffirmativeButton.KeyDown += affirmativeKeyHandler;

            this.KeyDown += escapeKeyHandler;

            PART_NegativeButton.Click    += negativeHandler;
            PART_AffirmativeButton.Click += affirmativeHandler;
            PART_ImportButton.Click      += importHandler;
            PART_NewProfileButton.Click  += newProfileHandler;

            return(tcs.Task);
        }