1

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

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

例如:

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

  • 另一个调整是我希望能够将出生日期格式化为仅显示MM/dd/yyyy.

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

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

4

1 回答 1

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;

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

于 2018-12-27T06:42:24.550 回答