2017-05-04 58 views
1

我們正在嘗試實施類似於確認字段的字段,該字段允許繼續/退出表單(而不是僅允許連續/更改表單字段的常規確認字段)。如何創建自定義確認按鈕

我們成功地做到了這一點,但唯一缺少的是能夠知道哪個是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自動()移動到下一個字段沒有我們不必指定下一個字段名

+0

你與框架的最新版本? –

回答

0

我以它的形式工作。

變化:

  1. new NextStep(nextField)給出編譯錯誤。變更爲:new NextStep(new []{nextField})
  2. 改變_next = delegateSetNext(delegate)
  3. 我加了一些方法和屬性將覆蓋領域。

    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(); 
    } 
    
+0

感謝您的迴應。我們希望NextStep()移動到下一個字段,而不必指定下一個字段名稱。你知道該怎麼做嗎? –