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