Ejemplo n.º 1
0
        public bool ShowViewModel(DialogViewModel viewModel, Func <AuthenticationDialogWindow> windowCreator)
        {
            if (!TryGetNext(out CapturedGuiOperation operation))
            {
                throw new ReplayNotFoundException($"Failed to find next `{nameof(CapturedGuiOperation)}`.");
            }
            if (!Ordinal.Equals(viewModel?.GetType().FullName, operation.DialogType))
            {
                throw new ReplayInputTypeException($"Expected `{viewModel?.GetType().FullName}` vs. Actual `{operation.DialogType}`.");
            }

            _context.Trace.WriteLine($"replay {nameof(ShowViewModel)}.");

            viewModel.IsValid = operation.Output.IsValid;
            viewModel.Result  = (AuthenticationDialogResult)operation.Output.Result;

            switch (viewModel)
            {
            case CredentialsViewModel cvm:
            {
                cvm.Login    = operation.Output.Login;
                cvm.Password = operation.Output.Password;
            }
            break;

            case TwoFactorViewModel tfvm:
            {
                tfvm.AuthenticationCode = operation.Output.AuthenticationCode;
            }
            break;
            }

            return(operation.Output.Success);
        }
    /// <summary>
    /// Show the required dialog.
    /// </summary>
    /// <typeparam name="TResult">The type of result to return.</typeparam>
    /// <param name="viewModel">The view model ascociated with the view.</param>
    public async Task <TResult> ShowDialog <TResult>(DialogViewModel <TResult> viewModel)
    {
        // Locate the ascociated view.
        var viewType = ViewLocator.LocateTypeForModelType(viewModel.GetType(), null, null);
        var dialog   = (BaseMetroDialog)Activator.CreateInstance(viewType);

        if (dialog == null)
        {
            throw new InvalidOperationException(
                      String.Format("The view {0} belonging to view model {1} " +
                                    "does not inherit from {2}",
                                    viewType,
                                    viewModel.GetType(),
                                    typeof(BaseMetroDialog)));
        }
        dialog.DataContext = viewModel;
        // Show the metro window.
        MetroWindow firstMetroWindow = Application.Current.Windows.OfType <MetroWindow>().First();
        await firstMetroWindow.ShowMetroDialogAsync(dialog);

        TResult result = await viewModel.Task;
        await firstMetroWindow.HideMetroDialogAsync(dialog);

        return(result);
    }
Ejemplo n.º 3
0
        private void Capture(bool success, DialogViewModel viewModel)
        {
            var capture = default(CapturedGuiOperation);

            switch (viewModel)
            {
            case CredentialsViewModel cvm:
            {
                capture = new CapturedGuiOperation
                {
                    Output = new CapturedGuiOutput
                    {
                        Login    = cvm.Login,
                        IsValid  = viewModel.IsValid,
                        Password = cvm.Password,
                        Result   = (int)viewModel.Result,
                        Success  = success,
                    },
                    DialogType = cvm.GetType().FullName,
                };
            }
            break;

            case TwoFactorViewModel tfvm:
            {
                capture = new CapturedGuiOperation
                {
                    Output = new CapturedGuiOutput
                    {
                        AuthenticationCode = tfvm.AuthenticationCode,
                        IsValid            = viewModel.IsValid,
                        Result             = (int)viewModel.Result,
                        Success            = success,
                    },
                    DialogType = tfvm.GetType().FullName,
                };
            }
            break;

            default:
                throw new ReplayDataException($"Unknown type `{viewModel.GetType().FullName}`");
            }

            lock (_syncpoint)
            {
                _captured.Enqueue(capture);
            }
        }