protected static Task <T> ShowDialogAsync <T>(object context, CloseableViewModelBase <T> viewModel, BaseMetroDialog view,
                                                      Action?onShown = null)
        {
            var metroWindow = GetMetroWindow(context);

            return(metroWindow.Invoke(() => metroWindow.ShowDialogAsync(viewModel, view, onShown)));
        }
Exemple #2
0
        /// <summary>
        /// Sets up the connection between view and viewModel, shows the view as a metro dialog,
        /// calls <paramref name="onShown"/> and waits for the dialog to be closed.
        /// </summary>
        public static async Task <T> ShowDialogAsync <T>(this MetroWindow window, CloseableViewModelBase <T> viewModel,
                                                         BaseMetroDialog view, Action?onShown = null)
        {
            view.DataContext = viewModel;

            // Undefault buttons not in the dialog as they would be pressed instead of a dialog button on enter.
            var oldDefaults = window.FindVisualChildren <Button>().Where(b => b.IsDefault).ToList();

            oldDefaults.ForEach(b => b.IsDefault = false);
            // Save old keyboard focus.
            var oldFocus = Keyboard.FocusedElement;

            await window.ShowMetroDialogAsync(view, new MetroDialogSettings { AnimateShow = false });

            // Focus the first focusable element in the view
            var element = view.FindVisualChildren <UIElement>().FirstOrDefault(e => e.Focusable);

            element?.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => Keyboard.Focus(element)));
            onShown?.Invoke();

            var result = await viewModel.WaitForCloseAsync();

            await window.HideMetroDialogAsync(view, new MetroDialogSettings { AnimateHide = false });

            // Restore IsDefault and keyboard focus.
            oldDefaults.ForEach(b => b.IsDefault = true);
            Keyboard.Focus(oldFocus);

            return(result);
        }