C# 立即执行操作
我创建了一个对话框:C# 立即执行操作,c#,botframework,C#,Botframework,我创建了一个对话框: [Serializable] public class StepOneDialog : IDialog<object> { // Private fields protected readonly IList<GroupResponseModel> _groups; protected readonly GroupResponseModel _group; protected readonly int _curren
[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);
}
在GetAnswer方法中,它将等待用户的响应,然后继续。我不希望它等待用户响应,我希望它只是执行。有人知道怎么做吗?我当时很傻。我刚刚将最后一行改为:
// Otherwise, ask our next question
} else
await AskQuestion(context, null);
由于在AskQuestion方法中未使用IAwaitable结果。。。是什么阻止您将该方法中的逻辑提取到一个不具有IAwaitable依赖项的新方法中,然后在else子句中调用该新方法?是的,您是对的。我只是那样做了。当我开始使用机器人时,我的大脑就不再思考正常的逻辑了……很好;不会将其作为答案发布;刚刚看到你做了。
// Otherwise, ask our next question
} else
await AskQuestion(context, null);