问题
I'm creating a PromptDialog Choice which populates my list of object Options stored in Database. However, it only displayed the type of the Object not the name of the Options. Can anyone suggest me the best way to load Option from database and binding it with the PromptDialog? Here's what I've tried:
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);
}
}
Here's my Option Object:
[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;
}
}
And here's the result I've gotten:
回答1:
Overriding the ToString() method of the Option object solves this.
The Microsoft botframework by default is using PromptStyle.Auto to print the options if you do not provide a PromptStyle as a parameter. So the botframework selects the style depending on the channel. For the emulator it seems to be text so it tries to print the option as a string.
回答2:
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;
}
public override string ToString()
{
return Text;
}
}
来源:https://stackoverflow.com/questions/42621534/promptdialog-choice-with-list-object-bot-framework