Xamarin网格适合循环的整个屏幕错误

时间:2018-12-14 02:52:09

标签: c# xamarin

我不太清楚我在这里缺少什么。目标:使用for循环创建一个5乘5的网格,以使列和行适合整个屏幕(因此GridUnitType.Star)。但是,我已经尝试了有关构建2D数组的所有知识,但是似乎没有正在工作。这些ColumnRow Definitions的工作方式是否不同?我想念什么?

NewGrid.RowDefinitions = new RowDefinitionCollection();
NewGrid.ColumnDefinitions = new ColumnDefinitionCollection();


for (int i = 0; i < textToInt; i++)
{
  NewGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  for (int j = 0; j < textToInt; j++)
  {
    NewGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
    NewGrid.Children.Add(new Label { Text = "" + i },j,i);
  }
}

2 个答案:

答案 0 :(得分:0)

            for (int i = 0; i < textToInt; i++)
            {
                NewGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
                NewGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
                for (int j = 0; j < textToInt; j++)
                {
                  
                    NewGrid.Children.Add(new Label { Text = "" +j},i,j);
                }
            }

答案 1 :(得分:0)

 for (int i = 0; i < textToInt; i++)
  {
    NewGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
 for (int j = 0; j < textToInt; j++)
 {
    NewGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  }
 }

 for (int i = 0; i < textToInt; i++)
 {
 for (int j = 0; j < textToInt; j++)
  {
    NewGrid.Children.Add(new Label { Text = "" + i },j,i);
  }
  }

尝试一下。