首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将BoxViews动态添加到网格[Xamarin.Forms]

将BoxViews动态添加到网格[Xamarin.Forms]
EN

Stack Overflow用户
提问于 2017-11-11 17:47:50
回答 1查看 2.5K关注 0票数 2

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

谢谢,

XAML

代码语言:javascript
运行
复制
<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#

代码语言:javascript
运行
复制
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 });
                }
            }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-11 22:44:48

当您将子元素添加到网格中时,您必须指定Row和Col,否则将在0,0添加它们。

代码语言:javascript
运行
复制
  imageGrid.Children.Add(new BoxView() { BackgroundColor = Color.Red }, newrow, newcol);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47240981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档