网格的动态组成问题

problem with the dynamic composition of a grid

我在 c# 中有一个异步函数,returns 我有一个 scrollView,其中在其中创建了一个由两个静态列组成的 Grid,并在 For 中动态创建了 Row。 如果在 For 中,我一次只创建一行,那么结果是正确的,但如果在 For 中,我尝试一次创建两行,则 Grid 以错误的方式组成,没有任何逻辑意义。

代码中的第一行在第 0 列中填充了一个标签,在第 1 列中填充了一个按钮,并且这些行由变量“p”分配,该变量在 For 的末尾递增。 相反,我想在第二行插入一个没有黑色背景文本的简单标签 此行也由变量“p + 1”动态分配。

好像它完全忽略了我想分配给第二行的内容,将其分配给循环期间创建的最后一行,第二个屏幕澄清了

我附上简化代码

 public async Task<Xamarin.Forms.ScrollView> GridComposionAsync()
    {
        Xamarin.Forms.ScrollView scrollView = new Xamarin.Forms.ScrollView();
        scrollView.Orientation = ScrollOrientation.Vertical;

        Grid grid = new Grid();
        
        grid.VerticalOptions = LayoutOptions.FillAndExpand;
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.7 , GridUnitType.Star)  });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.3, GridUnitType.Star) }); 
         

         int p = 0;
         int ts = Tempo / 10;

         for (int i = 0; i < 144; i += 0)
         {
             bool st = false;

  // in this part of the code checks are carried out for which the variable st can be true or false
                     
                 if (st == true)
                 {

                    // create first Row 

                   Label label = new Label
                   {
                         Text = $"è la riga n :{x.arrPre[i].Orario}" + "stato : " + st,
                         BackgroundColor = Color.Blue
                     };

                   Button button = new Button
                     {
                         Text = $"Button:{p}",
                         BackgroundColor = Color.Red,
                       
                     };
                   button.Clicked += Button_Clicked;
                
              
                   grid.RowDefinitions.Add(new RowDefinition { Height = 100 });
                   grid.Children.Add(label, 0, p);
                   grid.Children.Add(button, 1, p);

                  // create Second Row

                   grid.RowDefinitions.Add(new RowDefinition { Height = 10 });
                   Label label2row = new Label { BackgroundColor = Color.Black };
                   grid.Children.Add(label2row, 0, p + 1);

                   p++;
                   i += ts;
                 }
                 else
                 {                      
                     i++;
                 }
             }
         }));
        scrollView.Content = grid;
        return scrollView;  
    }

这是我的结果屏幕 ==> https://i.stack.imgur.com/34teL.png

第二个屏幕 ==> https://i.stack.imgur.com/E4zvG.png

因为您每次都插入两行,所以您应该为每个 p 添加 2

我不知道ts在你的代码中做了什么,所以我一次只插入两行,你可以根据需要更改它。

public async Task<Xamarin.Forms.ScrollView> GridComposionAsync()
    {
        Xamarin.Forms.ScrollView scrollView = new Xamarin.Forms.ScrollView();
        scrollView.Orientation = ScrollOrientation.Vertical;

        Grid grid = new Grid();

        grid.VerticalOptions = LayoutOptions.FillAndExpand;
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.7, GridUnitType.Star) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.3, GridUnitType.Star) });


        int p = 0;

        for (int i = 0; i < 10; i ++ )
        {
            bool st = true;

            // in this part of the code checks are carried out for which the variable st can be true or false

            if (st == true)
            {

                // create first Row 

                Label label = new Label
                {
                    Text = $"è la riga n : {i}" + "stato : " + st,
                    BackgroundColor = Color.Blue
                };

                Button button = new Button
                {
                    Text = $"Button:{i}",
                    BackgroundColor = Color.Red,

                };
                button.Clicked += Button_Clicked;


                grid.RowDefinitions.Add(new RowDefinition { Height = 100 });
                grid.Children.Add(label, 0, p);
                grid.Children.Add(button, 1, p);

            

                grid.RowDefinitions.Add(new RowDefinition { Height = 10 });
                Label label2row = new Label { BackgroundColor = Color.Black };
                grid.Children.Add(label2row, 0, p+1);
                p+=2;
            }
            else
            {
                i++;
            }
        }
        scrollView.Content = grid;
        return scrollView;
    }