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

分数。

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

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

例如:

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

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

我尝试使用模式语言试玩,但无法正常工作...我想要的是可能的吗?如果我手动创建对话框,我想展示如何将其与FormFlow的导航相关联?

自定义确认和后续导航菜单

凯尔·德莱尼(Kyle Delaney)

导航步骤的自定义有点棘手,因此我想出了一个解决方案,可让您解决此问题。诀窍是要确保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;

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章