动态地将BoxView添加到网格[Xamarin.Forms]

时间:2017-11-11 17:47:50

标签: c# ios xaml xamarin xamarin.forms

我正在尝试使用3列和多行以网格格式添加BoxView。我已经使用xaml和行为定义了网格 在c#文件中。应该发生的是应该为相同数量的图像创建BoxView,每列有3个图像。

谢谢,

XAML

<Grid RowSpacing="0" x:Name="scrollBarGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--Where the search bar will go-->
    <BoxView BackgroundColor="Aqua" Grid.Row="0"/>

        <SearchBar ></SearchBar>    

        <!--Where the images will go-->
    <BoxView BackgroundColor="Gray" Grid.Row="1"/>
    <Grid x:Name="imageGrid" RowSpacing="0" Grid.Row="1">

    </Grid>

</Grid>

C#

public MainPage()
    {
        InitializeComponent();

        int colMaximum = 3;
        int numberOfImages = 15;

        //To add three columns 
        for (int i = 0; i < colMaximum; i++)
        {
            imageGrid.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = new GridLength(120, GridUnitType.Absolute)
            });
        }

        //To add an array of rows
        imageGrid.RowDefinitions = new RowDefinitionCollection();

        for (int myCount = 0; myCount <= numberOfImages / colMaximum; myCount++)
        {
            imageGrid.RowDefinitions.Add(new RowDefinition()
            {
                Height = new GridLength(120, GridUnitType.Absolute)
            });

            //To add a new box view for each 
            for (int newcol = 0; newcol <= colMaximum; newcol++)
            {
                for (int newrow = 0; newrow <= numberOfImages / colMaximum; newrow++)
                {
                    imageGrid.Children.Add(new BoxView() { BackgroundColor = Color.Red });
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

将子项添加到网格时,必须指定Row和Col,否则它们将添加到0,0。

    int a2d[2][2]={{1,2},{3,4}};
    int ** pa;
    pa=a2d;