在Bot Framework中,在使用C#时,我有一个Dialog并使用PromptDialog.Choice让用户选择他们感兴趣的问题。
但是当它运行时,我得到了重复的回复,如下图所示:
public async Task StartAsync(IDialogContext context)
{
this.ShowOptions(context);
}
private void ShowOptions(IDialogContext context)
{
PromptDialog.Choice(
context,
this.OnOptionSelected,
new List() { ImageOption, ToolOption },
"Please select one of the following category.",
"Not a valid option",
3);
}
private async Task OnOptionSelected(IDialogContext context, IAwaitable result)
{
string optionSelected = await result;
switch (optionSelected)
{
case ImageOption:
context.Call(new ImgRelated(), this.ResumeAfterOptionDialog);
break;
case ToolOption:
context.Call(new ToolPBmDailog(), this.ResumeAfterOptionDialog);
break;
}
}
有人有想法吗?
答案 0 :(得分:2)
ShowOptions
需要是异步方法,需要在context.Wait(this.ShowOptions)
中使用StartAsync
进行调用,而不是直接调用它。
public async Task StartAsync(IDialogContext context)
{
context.Wait(this.ShowOptions);
}
public async virtual Task ShowOptions(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
PromptDialog.Choice<string>(
context,
this.OnOptionSelected,
new List() { ImageOption, ToolOption },
"Please select one of the following category.",
"Not a valid option",
3);
}
答案 1 :(得分:0)
您必须制作OnOptionSelected
方法,以便在执行之前等待用户的输入。另外,将ShowOptions
方法设置为异步,否则可能会出现异常,因为现在,ShowOptions
方法会向StartAsync
方法返回void。 IAwaitable<IMessageActivity>
等待用户的响应。请尝试以下操作,看看它是否适合您。
public async Task StartAsync(IDialogContext context)
{
await this.ShowOptions(context);
}
private async Task ShowOptions(IDialogContext context)
{
PromptDialog.Choice(context, this.OnOptionSelected,
new List() { ImageOption, ToolOption },
"Please select one of the following category.",
"Not a valid option", 3);
}
private async Task OnOptionSelected(IDialogContext context, IAwaitable<IMessageActivity> result)
{
string optionSelected = await result;
switch (optionSelected)
{
case ImageOption:
context.Call(new ImgRelated(), this.ResumeAfterOptionDialog);
break;
case ToolOption:
context.Call(new ToolPBmDailog(), this.ResumeAfterOptionDialog);
break;
}
}