本文介绍了机器人对话中未显示“反馈"文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Formflow对话框包含一个已应用自定义验证的字段...

My formflow dialog contains a field with custom validation applied...

 var form = builder
    .Field(new FieldReflector<CarValuationDialog>(nameof(UserName))
    .SetActive(state => string.IsNullOrEmpty(state.UserName)))
    .Field(new FieldReflector<CarValuationDialog>(nameof(ValuationOption))
        .SetPrompt(new PromptAttribute($"Hello {{UserName}}.<br /><br />Are you looking to get an price estimate for a car you’re selling, or for a car you’re looking to buy? {{||}}")))
    .Field(new FieldReflector<CarValuationDialog>(nameof(RegistrationNumber))
        .SetDefine(RegistrationNumberDefinitionMethod))
    .Field(new FieldReflector<CarValuationDialog>(nameof(Mileage))
                    .SetValidate(async (state, value) =>
                        {
                            var result = new ValidateResult { IsValid = true, Value = value };

                            if (int.TryParse(value.ToString(), out int mileage))
                            {
                                result.IsValid = true;
                            }
                            else
                            {
                                result.Feedback = "That isn't valid number. Can you enter it again please?";
                                result.IsValid = false;
                            }

                            return await Task.FromResult(result);
                        }))
    .Field(nameof(PreviousOwnerOption),
        active: carValuationDialog => carValuationDialog.ValuationOption == ValuationOptions.LookingToSell)
    .Field(nameof(ServiceHistoryOption),
        active: carValuationDialog => carValuationDialog.ValuationOption == ValuationOptions.LookingToSell)
    .Confirm(Confirmation)
    .OnCompletion(GetValuationAndDisplaySummaryToUser);

当执行SetValidate()且给定的里程值不是int时,将执行result.Feedback = ...行.但是,没有看到消息那不是有效的里程值.请您再输入一次?"我看到了...

When SetValidate() executes, and the given mileage value is not an int, the result.Feedback = ... line is executed. However, instead of seeing the message "That isn't a valid mileage value. Can you enter it again please?" I see this...

完全公开,我确实在对话框类中声明了以下属性

Full discolsure, I do have the following attribute declared on the dialog class

[Template(TemplateUsage.NotUnderstood, "Sorry, I don't understand '{0}'.")]

但是我想在这种情况下会向用户显示result.Feedback文本?如果用户输入无效的里程,则不使用Template[]会显示以下文本...

but I thought the result.Feedback text would be shown to the user in this case? Not using the Template[] results in the following text displayed when the user enters an invalid mileage...

哪个看起来很垃圾,这就是为什么我使用Template[]替代.

Which looks rubbish, and is why I used the Template[] override.

推荐答案

编辑我能够通过将模板应用于字段本身进行验证来实现此目的.这应该是您要查找的确切行为.请注意,我没有进行广泛的测试,我只是想让总体思路起作用.这是我使用的代码:

EDITI was able to accomplish this by applying templates to the fields themselves for validation. This should be the exact behavior you are looking for. Note that I did not do extensive testing, I just wanted to get the general idea working. Heres the code I used:

[Serializable]
public class SandwichOrder
{
    //public PreviousOwnerOptions Options;
    [Template(TemplateUsage.NotUnderstood, "Sorry, I don't understand '{0}'.")]
    [Prompt("What is the {&}?")]
    public int Mileage;

    [Template(TemplateUsage.NotUnderstood, "Sorry, This is not valid '{0}'.")]
    [Prompt("How many {&}?")]
    public int Days;


    public static IForm<SandwichOrder> BuildForm()
    {
        var form = new FormBuilder<SandwichOrder>()
            .Field(new FieldReflector<SandwichOrder>(nameof(Mileage))
                .SetValidate(async (state, value) =>
                {
                    var result = new ValidateResult {IsValid = true, Value = value};

                    if (int.TryParse(value.ToString(), out int mileage))
                    {
                        result.IsValid = true;
                    }
                    else
                    {
                        result.IsValid = false;
                    }

                    return await Task.FromResult(result);
                }))
            .Field(new FieldReflector<SandwichOrder>(nameof(Days))
            .SetValidate(async (state, value) =>
            {
                var result = new ValidateResult { IsValid = true, Value = value };

                if (int.TryParse(value.ToString(), out int days))
                {
                    result.IsValid = true;
                }
                else
                {
                    result.IsValid = false;
                }

                return await Task.FromResult(result);
            }));
        return form.Build();
    }
}

这产生了这个结果:

这篇关于机器人对话中未显示“反馈"文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:14