如何在bot框架C#中使用枚举类别和子类别? [英] How to use enum category and subcategory in bot framework C#?
问题描述
我正在研究一种机器人,其中有不同的类别和许多子类别. 我正在使用枚举来显示和收集输入.在这里,我只需要显示与上一步中选择的类别相关的子类别,我们如何才能实现这一目标.
I'm working on a bot where there are different categories and many sub categories . I am using enum to display and collect inputs. Here I need to display only the subcategories related to the category selected on the previous step how can we achieve this.
这是我正在使用的代码.
here is the code I'm working.
namespace ServiceDesk.Classes
{
public enum Category
{
hardware,
software,
email,
UserAdmin
};
public enum Subcategory
{
Desktop, KeyBoard, Laptop, Monitor, Mouse, Printer, Scanner, Server, Tablet
};
[Serializable]
public class HardwareQuery
{
[Prompt("Choose your {&} ? {||}")]
public Category? Categ;
[Prompt("Choose your {&} ? {||}")]
public Subcategory? SubCateg;
[Prompt("Please enter {&}")]
[Pattern(Utility.Phone)]
public string PhoneNumber { get; set; }
[Prompt("Please enter {&} ")]
[Pattern(Utility.Email)]
public string Email { get; set; }
[Prompt("Please provide your business need / {&} below")]
public string Justification { get; set; }
public static IForm<HardwareQuery> BuildForm()
{
return new FormBuilder<HardwareQuery>()
.Message("Welcome!")
.Build();
}
}
}
推荐答案
您可以使用FormBuilder流体方法动态定义表单.您可以找到有关执行此操作的文档
You can use the FormBuilder fluid methods to dynamically define your form. You can find docs on doing this here. In a nutshell, what you want to specifically look at is using a FieldReflector which will allow you setup an async delegate to build your dynamic SubCateg list.
您的BuildForm方法将最终看起来像这样:
Your BuildForm method will end up looking something like this:
public static IForm<HardwareQuery> BuildForm()
{
return new FormBuilder<HardwareQuery>()
.Message("Welcome!")
.Field(nameof(Categ))
.Field(new FieldReflector<HardwareQuery>(nameof(SubCateg))
.SetType(null)
.SetDefine(async (state, field) =>
{
//// Define your SubCateg logic here
switch (state.Categ)
{
Category.hardware:
break;
default:
break;
}
return true;
}))
.Field(nameof(PhoneNumber))
.Field(nameof(Email))
.Field(nameof(Justification))
.Build();
}
这篇关于如何在bot框架C#中使用枚举类别和子类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!