我创建了一个PromptDialog Choice,它填充了我在Database中存储的对象选项列表。但是,它仅显示对象的类型而不是选项的名称。任何人都可以建议我从数据库加载Option并将其与PromptDialog绑定的最佳方法吗?以下是我尝试过的内容:
private void ShowOptions(IDialogContext context)
{
List<Option> ListOptions = Option.CreateListOption();
PromptDialog.Choice(context, this.OnOptionSelected, ListOptions, "Are you looking for a flight or a hotel?", "Not a valid option", 3);
}
private async Task OnOptionSelected(IDialogContext context, IAwaitable<Option> result)
{
try
{
Option optionSelected = await result;
switch (optionSelected.Text )
{
case "A":
context.Call(new RootDialog(), this.ResumeAfterChoose);
break;
default: { context.Wait(MessageReceiveAsync); break; }
}
}
catch (TooManyAttemptsException ex)
{
await context.PostAsync($"Ooops! Too many attemps :(. But don't worry, I'm handling that exception and you can try again!");
context.Wait(this.MessageReceiveAsync);
}
}
这是
[Serializable]
public class Option
{
public int ID { get; set; }
public string Text { get; set; }
public Option()
{
ID = 0;
Text = "";
}
public static List<Option> CreateListOption()
{
List<Option> list = new List<Option>();
Option A = new Option();
A.ID = 1;
A.Text = "A";
Option B = new Option();
B.ID = 2;
B.Text = "B";
list.Add(A);
list.Add(B);
return list;
}
}
以下是我得到的结果:
答案 0 :(得分:0)
Choice
静态方法还包含Descriptions
参数。您可以尝试以下方法:
List<int>
答案 1 :(得分:0)
如Ezequiel所述,您可以提供Descriptions
参数,例如:
PromptDialog.Choice(context, this.OnOptionSelected,
ListOptions.Select(l=>l.ID).ToArray(),
"Are you looking for a flight or a hotel?", "Not a valid option", 3,
descriptions: ListOptions.Select(l=>l.Text).ToArray() );
然后将OnOptionSelected更改为:
private async Task OnOptionSelected(IDialogContext context, IAwaitable<int> result)
{
try
{
int optionSelected = await result;
var foundOption = Option.CreateListOption().First(o => o.ID == optionSelected);
//do more with foundOption
switch (optionSelected)
{
case 1:
context.Call(new RootDialog(), this.ResumeAfterChoose);
break;
default: { context.Wait(MessageReceivedAsync); break; }
}
}
catch (TooManyAttemptsException ex)
{
await context.PostAsync($"Ooops! Too many attemps :(. But don't worry, I'm handling that exception and you can try again!");
context.Wait(this.MessageReceivedAsync);
}
}