网格的动态组合问题

problem with the dynamic composition of a grid

提问人:Salvatore Lorello 提问时间:7/7/2021 更新时间:7/8/2021 访问量:53

问:

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

代码中的第一行在第 0 列中填充了 Label,在第 1 列中填充了 Button,并且 with 和行由变量 “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

C# Xamarin.Forms 网格

评论


答:

0赞 Leo Zhu 7/8/2021 #1

因为你每次都插入两行,所以你应该为每行加号。2p

我不知道你的代码中有什么作用,所以我一次只插入两行,如果你愿意,你可以改变它。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;
    }

评论

0赞 Salvatore Lorello 7/8/2021
谢谢!!那个粗心大意,我没有完全注意到,再次💪感谢