0
我寫這個方法現在的3倍左右,我敢肯定我不會做書寫了一段時間:通用網格填充擴展方法
public void PopulateField() {
this.grdNameField.ColumnDefinitions.Clear();
this.grdNameField.RowDefinitions.Clear();
this.grdNameField.Children.Cast<Viewbox>().ToList().ForEach(VB => VB.Child = null);
this.grdNameField.Children.Clear();
for (int x = 0; x < Settings.Default.PlayerCount; x++) {
Viewbox VB = new Viewbox() { Child = this.PlayerNamers[x], Stretch = Stretch.Uniform };
this.grdNameField.Children.Add(VB);
Grid.SetColumn(VB, x % Math.Max(3, (int)(Math.Ceiling(Settings.Default.PlayerCount/5.0D))));
Grid.SetRow(VB, x/Math.Max(3, (int)(Math.Ceiling(Settings.Default.PlayerCount/5.0D))));
}
int
ColumnCount = this.grdNameField.Children.Cast<Viewbox>().Max(VB => Grid.GetColumn(VB)) + 1,
RowCount = this.grdNameField.Children.Cast<Viewbox>().Max(VB => Grid.GetRow(VB)) + 1;
for (int x = 0; x < ColumnCount; x++)
this.grdNameField.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
for (int x = 0; x < RowCount; x++)
this.grdNameField.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
}
我終於預告三個函數之間的區別僅僅是網格被填充的東西:一個List對象的通用列表(有時候,Viewbox Stretch屬性被設置爲什麼)。
我想編寫一個通用的方法來代替這三個(和所有未來的)方法,我有一個想法如何做到這一點?