简体   繁体   English

如何在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();
        }
    }
}

You can use the FormBuilder fluid methods to dynamically define your form. 您可以使用FormBuilder流体方法来动态定义表单。 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. 简而言之,您要专门查看的是使用FieldReflector,它将允许您设置异步委托来构建动态SubCateg列表。

Your BuildForm method will end up looking something like this: 您的BuildForm方法将最终看起来像这样:

 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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM