简体   繁体   English

在 Xamarin.Forms 中动态创建网格

[英]Creating grids dynamically in Xamarin.Forms

I am currently working an test application to list eg Products, but have the problem of not being able to generate the grids dynamically with the corresponding content (for now only labels).我目前正在开发一个测试应用程序来列出例如产品,但存在无法使用相应内容动态生成网格的问题(目前只有标签)。 I want to generate them right when the Mainpage is called.我想在调用 Mainpage 时立即生成它们。

I have already gone throughout various tutorials and websites, but couldn't find anything that would help me save my problem.我已经浏览了各种教程和网站,但找不到任何可以帮助我解决问题的方法。 Tried to start the method for creating the grids by assigning it to a button.试图通过将其分配给按钮来启动创建网格的方法。 I have tried allocating the method to the constructor of the MainPage class, but still there won't be anything shown in the final result.我已经尝试将方法分配给MainPage class的构造函数,但最终结果仍然没有显示任何内容。

public void CreateDummyGrids()
    {
        Grid gOut = new Grid();

        gOut.RowDefinitions.Add(new RowDefinition());
        gOut.RowDefinitions.Add(new RowDefinition());
        gOut.RowDefinitions.Add(new RowDefinition());
        gOut.ColumnDefinitions.Add(new ColumnDefinition());
        gOut.ColumnDefinitions.Add(new ColumnDefinition());

        for (int rowIndex = 0; rowIndex < 3; rowIndex++)
        {
            for (int columnIndex = 0; columnIndex < 2; columnIndex++)
            {

                var label = new Label
                {
                   Text  ="Hello",
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalOptions = LayoutOptions.Center
                };
                gOut.Children.Add(label, columnIndex, rowIndex);
            }
        }
    }

The Grid gOut is a local variable to your CreateDummyGrids method and not passed to anywhere else. Grid gOutCreateDummyGrids方法的局部变量,不会传递给其他任何地方。 So after the method it'll just get destroyed. 因此,使用该方法后,它将被销毁。

You need to have somekind of element in your XAML to add the grid to (or just place the grid there and add the children directly to that). 您需要在XAML中具有某种元素才能将网格添加到其中(或只是将网格放置在那里并将子代直接添加到该网格中)。

So in the place where you want the grid to appear add something like this: 因此,在您希望网格出现的位置添加以下内容:

<Grid x:Name="productGrid" />

And change your CreateDummyGrids to this: 并将您的CreateDummyGrids更改为此:

public void CreateDummyGrids()
    {
        productGrid.RowDefinitions.Add(new RowDefinition());
        productGrid.RowDefinitions.Add(new RowDefinition());
        productGrid.RowDefinitions.Add(new RowDefinition());
        productGrid.ColumnDefinitions.Add(new ColumnDefinition());
        productGrid.ColumnDefinitions.Add(new ColumnDefinition());

        for (int rowIndex = 0; rowIndex < 3; rowIndex++)
        {
            for (int columnIndex = 0; columnIndex < 2; columnIndex++)
            {

                var label = new Label
                {
                   Text  ="Hello",
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalOptions = LayoutOptions.Center
                };
                productGrid.Children.Add(label, columnIndex, rowIndex);
            }
        }
    }

Now keep in mind that everytime you call this method new ColumnDefinitions, RowDefinitions and Labels will be added, so if you want you want to keep adding stuff you have to change the setup a bit. 现在请记住,每次调用此方法时,都会添加新的ColumnDefinitions,RowDefinitions和Labels,因此,如果要继续添加内容,则必须稍作更改。 But I hope this is enough to get you in the right direction 但我希望这足以使您朝正确的方向前进

        Grid dynamicGrid = new Grid { Padding= new Thickness(5,5)};

        dynamicGrid.RowDefinitions.Add(new RowDefinition()); 
        dynamicGrid.ColumnDefinitions.Add(new ColumnDefinition());
        dynamicGrid.ColumnDefinitions.Add(new ColumnDefinition());

        var label = new Label();
        var entry= new Entry();
        dynamicGrid.Children.Add(label, 0, 0);
        dynamicGrid.Children.Add(entry, 1, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM