private ChoicePromptHandler CreateChoicePromptHandler() { if (this.activePromptHandler != null) { Logger.LogError( "Prompt handler requested while another prompt is already active."); } ChoicePromptHandler choicePromptHandler = this.OnCreateChoicePromptHandler(); this.activePromptHandler = choicePromptHandler; this.activePromptHandler.PromptCancelled += activePromptHandler_PromptCancelled; return(choicePromptHandler); }
/// <summary> /// Prompts the user to make a choice using the provided details. /// </summary> /// <param name="promptCaption"> /// The caption string which will be displayed to the user. /// </param> /// <param name="promptMessage"> /// The descriptive message which will be displayed to the user. /// </param> /// <param name="choices"> /// The list of choices from which the user will select. /// </param> /// <param name="defaultChoice"> /// The default choice to highlight for the user. /// </param> /// <param name="cancellationToken"> /// A CancellationToken that can be used to cancel the prompt. /// </param> /// <returns> /// A Task instance that can be monitored for completion to get /// the user's choice. /// </returns> public Task <int> PromptForChoiceAsync( string promptCaption, string promptMessage, ChoiceDetails[] choices, int defaultChoice, CancellationToken cancellationToken) { // TODO: Guard against multiple calls this.Caption = promptCaption; this.Message = promptMessage; this.Choices = choices; this.DefaultChoices = defaultChoice == -1 ? Array.Empty <int>() : new int[] { defaultChoice }; // Cancel the TaskCompletionSource if the caller cancels the task cancellationToken.Register(this.CancelPrompt, true); // Convert the int[] result to int return(this.WaitForTaskAsync( this.StartPromptLoopAsync(this.promptCancellationTokenSource.Token) .ContinueWith( task => { if (task.IsFaulted) { throw task.Exception; } else if (task.IsCanceled) { throw new TaskCanceledException(task); } return ChoicePromptHandler.GetSingleResult(task.GetAwaiter().GetResult()); }))); }