C# 如何防止在确认提示中显示问题?
我不想在最后的确认中显示一个问题C# 如何防止在确认提示中显示问题?,c#,botframework,bots,formflow,C#,Botframework,Bots,Formflow,我不想在最后的确认中显示一个问题 我可以想出三种方法。作为一个例子,我将使用一个名为FirstName、LastName和MiddleName的表单,MiddleName是要隐藏的字段 选择1 您可以自定义确认步骤,以明确列出除一个字段外的每个字段: public static IForm<MyClass> BuildForm() { var fieldNames = new[] { nameof(FirstName), nameof(MiddleName), nameof(
我可以想出三种方法。作为一个例子,我将使用一个名为FirstName、LastName和MiddleName的表单,MiddleName是要隐藏的字段 选择1 您可以自定义确认步骤,以明确列出除一个字段外的每个字段:
public static IForm<MyClass> BuildForm()
{
var fieldNames = new[] { nameof(FirstName), nameof(MiddleName), nameof(LastName) };
var confirmation = new StringBuilder("Is this your selection?");
var formBuilder = new FormBuilder<MyClass>();
foreach (var name in fieldNames)
{
// Add the field to the form
formBuilder.Field(name);
// Add the field to the confirmation prompt unless it's MiddleName
if (name != nameof(MiddleName))
{
confirmation.AppendLine().Append($"* {{&{name}}}: {{{name}}}");
}
}
formBuilder.Confirm(new PromptAttribute(confirmation.ToString()) {
FieldCase = CaseNormalization.None
});
return formBuilder.Build();
}
选择3
你可以用一个小盒子。这是一个有点先进,但它给了你最多的控制
public static IForm<MyClass> BuildForm()
{
var formBuilder = new FormBuilder<MyClass>()
.Prompter(PromptAsync)
;
return formBuilder.Build();
}
/// <summary>
/// Here is the method we're using for the PromptAsyncDelgate.
/// </summary>
private static async Task<FormPrompt> PromptAsync(IDialogContext context,
FormPrompt prompt, MyClass state, IField<MyClass> field)
{
var preamble = context.MakeMessage();
var promptMessage = context.MakeMessage();
// Check to see if the form is on the confirmation step
if (field.Name.StartsWith("confirmation"))
{
// If it's on the confirmation step,
// we want to remove the MiddleName line from the text
var lines = prompt.Prompt.Split('\n').ToList();
var middleNameField = field.Form.Fields.Field(nameof(MiddleName));
var format = new Prompter<MyClass>(
middleNameField.Template(TemplateUsage.StatusFormat), field.Form, null);
var middleNameLine = "* " + format.Prompt(state, middleNameField).Prompt;
lines.RemoveAll(line => line.StartsWith(middleNameLine));
prompt.Prompt = string.Join("\n", lines);
}
if (prompt.GenerateMessages(preamble, promptMessage))
{
await context.PostAsync(preamble);
}
await context.PostAsync(promptMessage);
return prompt;
}
非常感谢,我会努力的。效果很好。
[Optional]
public string MiddleName { get; set; }
public static IForm<MyClass> BuildForm()
{
NextDelegate<MyClass> next = (value, state) =>
{
// Make sure MiddleName is active during most steps
state._isMiddleNameActive = true;
return new NextStep();
};
var formBuilder = new FormBuilder<MyClass>()
.Field(new FieldReflector<MyClass>(nameof(FirstName)).SetNext(next))
.Field(new FieldReflector<MyClass>(nameof(MiddleName)).SetNext(next)
.SetActive(state => state._isMiddleNameActive))
.Field(new FieldReflector<MyClass>(nameof(LastName)).SetNext(next))
;
formBuilder.Confirm(async state =>
{
// Make sure MiddleName is inactive during the confirmation step
state._isMiddleNameActive = false;
// Return the default confirmation prompt
return new PromptAttribute(
formBuilder.Configuration.Template(TemplateUsage.Confirmation));
});
return formBuilder.Build();
}
// This private field isn't included in the form but is accessible via the form's state
private bool _isMiddleNameActive;
public static IForm<MyClass> BuildForm()
{
var formBuilder = new FormBuilder<MyClass>()
.Prompter(PromptAsync)
;
return formBuilder.Build();
}
/// <summary>
/// Here is the method we're using for the PromptAsyncDelgate.
/// </summary>
private static async Task<FormPrompt> PromptAsync(IDialogContext context,
FormPrompt prompt, MyClass state, IField<MyClass> field)
{
var preamble = context.MakeMessage();
var promptMessage = context.MakeMessage();
// Check to see if the form is on the confirmation step
if (field.Name.StartsWith("confirmation"))
{
// If it's on the confirmation step,
// we want to remove the MiddleName line from the text
var lines = prompt.Prompt.Split('\n').ToList();
var middleNameField = field.Form.Fields.Field(nameof(MiddleName));
var format = new Prompter<MyClass>(
middleNameField.Template(TemplateUsage.StatusFormat), field.Form, null);
var middleNameLine = "* " + format.Prompt(state, middleNameField).Prompt;
lines.RemoveAll(line => line.StartsWith(middleNameLine));
prompt.Prompt = string.Join("\n", lines);
}
if (prompt.GenerateMessages(preamble, promptMessage))
{
await context.PostAsync(preamble);
}
await context.PostAsync(promptMessage);
return prompt;
}