如何创建自定义确认按钮

时间:2017-05-04 10:36:53

标签: c# botframework

我们正在尝试实现类似于确认字段的字段,该字段允许继续/退出表单(而不是仅允许继续/更改表单字段的常规确认字段)。

我们设法使这项工作做得很好,但唯一缺少的是能够知道quitForm字段之后的下一个字段。

当我们调用NextStep()时,表单退出而不是像确认一样移动到下一个字段。

我们如何模仿确认的行为?

这是我们的实现,它与Confirmation.cs非常相似:

public class QuitFormField<T> : Field<T> where T : class
{
        private const string c_continueTerm = "Continue";
        private const string c_quitTerm = "Quit";

        public QuitFormField(
            string prompt,
            string nextField,
            ActiveDelegate<T> condition, 
            IEnumerable<string> dependencies) : base(name: Guid.NewGuid().ToString(), role:FieldRole.Confirm)
        {
            SetPrompt(new PromptAttribute(prompt));
            SetDefine((state, field) =>
            {
                field.AddDescription(value: c_continueTerm, description: c_continueTerm).AddTerms(value: c_continueTerm, terms: c_continueTerm);
                field.AddDescription(value: c_quitTerm, description: c_quitTerm).AddTerms(value: c_quitTerm, terms: c_quitTerm);
                return Task.FromResult(true);
            });

            SetType(null);
            SetDependencies(dependencies?.ToArray());
            SetActive(condition);
            _next = (value, state) => (string)value == c_continueTerm ? new NextStep(nextField) : new NextStep(StepDirection.Quit);

        }
}

澄清:我们希望NextStep()移动到下一个字段,而不必指定下一个字段名称

1 个答案:

答案 0 :(得分:0)

我让它以我的形式工作。

变更:

  1. new NextStep(nextField)给出了编译错误。已更改为:new NextStep(new []{nextField})
  2. _next = delegate更改为SetNext(delegate)
  3. 我为Field添加了一些方法和属性覆盖。

    private const string c_continueTerm = "Continue";
    private const string c_quitTerm = "Quit";
    
    public QuitFormField(
        string prompt,
        string nextField,
        ActiveDelegate<T> condition,
        IEnumerable<string> dependencies) : base(name: Guid.NewGuid().ToString(), role: FieldRole.Confirm)
    {
        SetPrompt(new PromptAttribute(prompt));
        SetDefine((state, field) =>
        {
            field.AddDescription(value: c_continueTerm, description: c_continueTerm).AddTerms(value: c_continueTerm, terms: c_continueTerm);
            field.AddDescription(value: c_quitTerm, description: c_quitTerm).AddTerms(value: c_quitTerm, terms: c_quitTerm);
            return Task.FromResult(true);
        });
    
        SetType(null);
        SetDependencies(dependencies?.ToArray());
        SetActive(condition);
        SetNext((value, state) => (string)value == c_continueTerm ? new NextStep(new []{nextField}) : new NextStep(StepDirection.Quit));
    }
    
    public override object GetValue(T state)
    {
        return null;
    }
    
    public override IEnumerable<string> Dependencies => _dependencies;
    
    public override bool Active(T state)
    {
        return _condition(state);
    }
    
    public override NextStep Next(object value, T state)
    {
        return _next((string)value, state);
    }
    
    public override void SetValue(T state, object value)
    {}
    
    public override bool IsUnknown(T state)
    {
        return true;
    }
    
    public override void SetUnknown(T state)
    {
        throw new NotImplementedException();
    }