2016-11-04 6 views
0

私は剣道のUI MVCグリッドを使用しています。定型コードをカプセル化して、すべてのグリッドで同じコードを複製する必要はありません。グリッド上でコマンドを設定すると、次のようになります。ラムダ式の{}内にデフォルト式を追加するにはどうすればよいですか?

columns.Command(command => 
      { 
       command.Custom("Edit").Text("<span class='k-icon k-edit'></span>").Click("editRecord"); 
       command.Custom("Delete").Text("<span class='k-icon k-i-delete'></span>").Click("deleteItem"); 
      }).Width(130); 

編集、削除は、しかし、グリッドに応じて、追加のカスタムコマンドの可能性がある、定型です。コマンドのラムダのタイプはAction<GridActionCommandFactory<T>>です。カスタムコマンドを入力しながら、ボイラープレートをメソッドまたは何かに抽象化するにはどうすればよいですか?

columns.Command(command => 
      { 
       //Custom commands here 
       SomeConfigClass.DefaultGridCommands(command); 
       //Custom commands here 
      }).Width(130); 

または多分:

columns.Command(command => 
      { 
       //Custom commands here 
       command.DefaultCommands(); 
       //Custom commands here 
      }).Width(130); 

そして、これが編集を含めるとコマンドを削除します擬似コーディングそれを私はそれが次のようになります把握します。しかし、ラムダ式をこのように変更する方法はわかりませんが、どうすればこのことができますか?

+0

ここで 'command'パラメータの型は何ですか?columns.Command(command =>'?つまり、 'Action >' –

+0

は問題ではありません。 – SventoryMang

答えて

0

もう少し掘り下げてしまったので、それほど難しくはありませんでした。わからないことが最もエレガントなソリューションだが、私はこのようにそれをしなかった場合:

public static Action<GridActionCommandFactory<T>> GetDefaultGridCommands<T>(Action<GridActionCommandFactory<T>> customCommandsBeforeDefault = null, Action<GridActionCommandFactory<T>> customCommandsAfterDefault = null) where T : class 
    { 
     Action<GridActionCommandFactory<T>> defaultCommands = x => 
     { 
      x.Custom("Edit").Text("<span class='k-icon k-edit'></span>").Click("editRecord"); 
      x.Custom("Delete").Text("<span class='k-icon k-i-delete'></span>").Click("deleteItem"); 
     }; 

     List<Action<GridActionCommandFactory<T>>> actions = new List<Action<GridActionCommandFactory<T>>>(); 

     if(customCommandsBeforeDefault != null) 
      actions.Add(customCommandsBeforeDefault); 
     actions.Add(defaultCommands); 
     if(customCommandsAfterDefault != null) 
      actions.Add(customCommandsAfterDefault); 

     Action<GridActionCommandFactory<T>> combinedAction = (Action<GridActionCommandFactory<T>>) Delegate.Combine(actions.ToArray()); 

     return combinedAction; 
    } 

次にグリッドでそれを呼び出す:

columns.Command(KendoUiGridConfig.GetDefaultGridCommands<MyViewModel>()).Width(130); 

Delegate.Combine方法は、私が探していたものでした。

関連する問題