2016-11-04 69 views
0

我使用的是Kendo UI MVC Grid,我想封裝樣板代碼,所以我不必在每個網格上重複相同的代碼。對電網配置命令如下:如何在lambda表達式的{}中提供默認表達式,同時仍允許將其添加到?

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); 

編輯和刪除的樣板,但有是根據電網多餘的自定義命令的可能性。 lambda命令的類型爲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); 

,這將包括編輯和刪除命令。但我不知道如何以這種方式修改lambda表達式,我該如何實現呢?

+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方法就是我一直在尋找。