如何使用 FormFlow 向用户动态呈现值列表

How can I present a list of values dynamically to the user with FormFlow

我想使用 FormFlow 对话框,但我看到的所有示例都会提示用户从 Enum 中进行选择。如果值来自数据库怎么办?

如果您想为 FormFlow 中的字段动态设置值,您必须使用动态定义的字段。

这是文档和完整的用法示例:https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-formflow-formbuilder?view=azure-bot-service-3.0

基本上,您必须使用 [=25= 在 SetDefine 函数中获取数据(来自 SQL 数据库或其他任何地方) ]field.AddTerms 函数。

:)

这里演示了如何在 formflow 中实现来自数据库的动态字段。 只需将 StateClass 替换为您的 class 的名称,并将 ProperyName 替换为您的 属性 的名称。不幸的是,目前您在 Microsoft Bot Framework 的官方文档中找不到任何有关 FieldReflector 的信息。

    public static IForm<StateClass> BuildForm()
    {
        return new FormBuilder<StateClass>()
                .Message("Start Message")
                .Field(new FieldReflector<StateClass>(nameof(ProperyName))
                        .SetType(null)
                        .SetDefine(async (state, field) =>
                        {
                            List<string> list = QueryFromDatabase();
                            foreach (string item in list)
                                field
                                    .AddDescription(item , item )
                                    .AddTerms(item , item );

                            return true;
                        }))
                .AddRemainingFields()
                .OnCompletionAsync(async (context, state) => 
                 {
                    await context.PostAsync("Finish message");
                 })
                .Build();
    }