【问题标题】:Microsoft Bot Framework: How can I populate form flow field values based on the user's input for a previous fieldMicrosoft Bot Framework:如何根据用户对前一个字段的输入来填充表单流字段值
【发布时间】:2018-10-04 15:40:03
【问题描述】:
对于表单流案例,我有以下属性:
public enum Offices{}
[Describe("Country")]
public string Country;
[Prompt("Which office are you working in?{||}")]
public Offices Office;
我想根据指定的国家/地区填充办公室。
例如,如果用户输入 India 作为 Country 字段,我希望办公室是 Mumbai、New Delhi 和 Pune。如果用户进入阿联酋,我希望办公室是迪拜和阿布扎比等......
我怎样才能做到这一点?
【问题讨论】:
标签:
botframework
formflow
dynamic-attributes
【解决方案1】:
这是一个与“How to use enum category and subcategory in bot framework C#?”类似的问题,至少在如何做你需要的事情上。
使用 FormBuilder,您可以动态定义您的表单。 FormBuilder 上的完整文档是here。
回顾上一个 StackOverlfow 答案,您使用 FieldReflector,这将允许您设置异步委托。在该委托中,您将根据 state.Country 值构建城市列表。
它看起来像这样:
public static IForm<Offices> BuildForm()
{
return new FormBuilder<Offices>()
.Message("Welcome!")
.Field(nameof(Country))
.Field(new FieldReflector<Offices>(nameof(Office))
.SetType(null)
.SetDefine(async (state, field) =>
{
//// Define your Officelogic here
switch (state.Country)
{
Country.Dubai:
////logic to add Dubai city
break;
Country.UAE:
////logic to add UAE cities
break;
default:
break;
}
return true;
}))
.Build();
}