立即执行操作

本文关键字:操作 执行 | 更新日期: 2023-09-27 18:18:31

我创建了这个对话框:

[Serializable]
public class StepOneDialog : IDialog<object>
{
    // Private fields
    protected readonly IList<GroupResponseModel> _groups;
    protected readonly GroupResponseModel _group;
    protected readonly int _currentStep;
    protected int _currentQuestion = 0;
    /// <summary>
    /// Default constructor
    /// </summary>
    /// <param name="groups">The question groups</param>
    public StepOneDialog(IList<GroupResponseModel> groups, int step)
    {
        _groups = groups;
        _group = groups[step];
        _currentStep = step;
    }
    /// <summary>
    /// Start our response
    /// </summary>
    /// <param name="context">The current context</param>
    /// <returns></returns>
    public async Task StartAsync(IDialogContext context) => context.Wait(AskQuestion);
    /// <summary>
    /// When our message is recieved we execute this delegate
    /// </summary>
    /// <param name="context">The current context</param>
    /// <param name="result">The result object</param>
    /// <returns></returns>
    private async Task AskQuestion(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        // Get our question and answers
        var question = this._group.Questions[_currentQuestion];
        var questionText = question.Text;
        var answers = question.Answers.Select(m => m.Text).ToList();
        var options = new PromptOptions<string>(questionText, options: answers);
        // If wer are the first question AND we have more than 1 question, Post the question header
        if (_currentQuestion == 0 && _group.Questions.Count > 1)
            await context.PostAsync(_group.Text);
        // Ask our question
        Choice<string>(context, GetAnswer, options);
    }
    /// <summary>
    /// Get our answer and decide what to do next
    /// </summary>
    /// <param name="context">The current context</param>
    /// <param name="result">The answer text</param>
    /// <returns></returns>
    private async Task GetAnswer(IDialogContext context, IAwaitable<string> result)
    {
        // Get our quest
        var questions = _group.Questions;
        var length = questions.Count;
        var question = _group.Questions[_currentQuestion];
        // Assign our answer to our question
        foreach (var answer in question.Answers)
            if (answer.Text == await result)
                question.Answer = answer;
        // Increase our index
        _currentQuestion++;
        // If our current index is greater or equal than the length of the questions
        if (_currentQuestion == length)
        {
            // If our step is the same as our group length
            var groupLength = _groups.Count - 1;
            // If we have reached the end of our steps, this dialog is done
            if (groupLength == _currentStep)
                context.Wait(ResumeAfter);
            // Otherwise, got to the next step
            await context.Forward(new StepOneDialog(_groups, _currentStep + 1), ResumeAfter, new Activity { }, CancellationToken.None);
      // Otherwise, ask our next question
        } else
            context.Wait(AskQuestion);
    }
    /// <summary>
    /// When the child dialog has complete, mark this as done
    /// </summary>
    /// <param name="context">The current context</param>
    /// <param name="result">The result object</param>
    /// <returns></returns>
    private async Task ResumeAfter(IDialogContext context, IAwaitable<object> result) => context.Done(this);
}

有三个步骤,所以当第一步开始时,会提出一个问题。当用户回答问题时(因为在第一步中只有一个问题),将启动一个新的对话实例,并提出新一轮的问题。在这种情况下,有不止一个问题,所以当用户回答第一个问题时,我希望它立即问第二个问题。问题是我有这个:

// Otherwise, ask our next question
} else
    context.Wait(AskQuestion);

GetAnswer方法中,它将在继续之前等待用户的响应。我不想让它等待用户响应,我只想让它执行。有人知道怎么做吗?

立即执行操作

我在装傻。我只是把最后一行改成这样:

// Otherwise, ask our next question
} else
    await AskQuestion(context, null);