在表单的确认对话框之后自定义导航表单

时间:2018-12-13 21:46:10

标签: c# botframework formflow

我已经定义了一个自定义MessageDelegate,以传递给我的.Confirm(...)上的FormBuilder。 (请参见屏幕截图。)

我的问题是我想自定义当用户在Confirm对话框中选择“否”时出现的“导航”菜单。我发现this帖子似乎朝着正确的方向前进,但我想进行更多自定义。我仍然希望显示按钮列表,但是我希望能够指定显示/不显示哪些按钮,以及每个按钮上的文本,而不是由FormFlow自动填充。

例如:

  • 在我的用例中,我有一个HasMiddleName字段,后跟一个MiddleName字段,只有在HasMiddleName字段收到“是”答案时,该字段才对用户显示。我希望导航仅显示“中间名”,类似于其显示“姓/名”的方式。而且,如果用户选择了中间名,我希望它重定向到表单的HasMiddleName部分。

  • 另一个调整是,我希望能够格式化“出生日期”以仅显示MM/dd/yyyy

我尝试过使用Pattern language,但无法正常工作...我想要的可能吗?如果我手动创建对话框,我想显示如何将其与FormFlow的导航相关联?

Custom Confirm and Subsequent Navigation Menu

1 个答案:

答案 0 :(得分:1)

自定义导航步骤有些棘手,因此我想出了一个解决方案,可让您解决该问题。诀窍是要确保在导航步骤滚动时MiddleName字段处于非活动状态,并使HasMiddleName字段伪装成MiddleName字段,以便单击它可以将您带到HasMiddleName字段。

// We want our HasMiddleName field to be treated as the "Middle Name" field for navigation purposes
[Describe("Middle Name"), Prompt("Does the dependent have a middle name?"), Template(TemplateUsage.NavigationFormat, "{&}({MiddleName})", FieldCase = CaseNormalization.None)]
public bool HasMiddleName { get; set; }

// I'm showing you how to use the "Unspecified" template but for some reason it doesn't work in the navigation step.
// Also, be careful about giving two fields the same description. It works in this case because of the tricks we're using.
[Optional, Describe("Middle Name"), Prompt("Please enter middle name {||}"), Template(TemplateUsage.NoPreference, "None"), Template(TemplateUsage.Unspecified, "None")]
public string MiddleName { get; set; }

[Template(TemplateUsage.NavigationFormat, "{&}({:d})", FieldCase = CaseNormalization.None)]
public DateTime DateOfBirth { get; set; }

public static IForm<MyClass> BuildForm()
{
    var builder = new FormBuilder<MyClass>()
        .Field(new FieldReflector<MyClass>(nameof(HasMiddleName)).SetNext((value, state) =>
        {
            // This NextDelegate will execute after the user enters a value for HasMiddleName
            bool didTheySayYes = (bool)value;
            // If MiddleName is inactive it will be skipped over
            state._isMiddleNameActive = didTheySayYes;

            if (didTheySayYes)
            {
                // We need to explicitly navigate to the MiddleName field
                // or else it will go back to the confirmation step
                // if a middle name had already been entered
                return new NextStep(new[] { nameof(MiddleName) });
            }
            else
            {
                // We want to clear the middle name in case one had been entered before
                state.MiddleName = null;
                // This will go to either the DateOfBirth field or the confirmation step
                // since the MiddleName field will be inactive in this case
                return new NextStep();
            }
        }))
        .Field(new FieldReflector<MyClass>(nameof(MiddleName)).SetActive(state => state._isMiddleNameActive))
        .Field(new FieldReflector<MyClass>(nameof(DateOfBirth)))
        .Confirm(async state =>
        {
            // We're making sure MiddleName is inactive at the confirmation step
            // so it won't be visible in the navigation step,
            // but since we're not changing the MiddleName field
            // it can still be retrieved from the form's result
            state._isMiddleNameActive = false;
            return new PromptAttribute("Ok. Is this correct? {||}");
        });

    return builder.Build();
}

// This private field isn't included in the form
private bool _isMiddleNameActive;

另一方面,还有另一种方法可以探索您是否真的要控制导航步骤中显示的按钮。上面的代码可以很好地为您服务,但是我觉得我应该简要地提到另一个技巧,因为我写了一个post来说明如何做。如果您自己创建提示,则可以检查__navigation__委托中的字段名称是否等于PromptAsync,然后相应地生成一条消息。请记住,您仍然必须使用某些第一种解决方案来确保单击“中间名”将您带到HasMiddleName字段。