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

标签 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 });
                }
            }
        }
    }

最佳答案

当您向网格添加子项时,您必须指定行和列,否则它们将添加到 0,0。

  imageGrid.Children.Add(new BoxView() { BackgroundColor = Color.Red }, newrow, newcol);

关于c# - 动态地将 BoxView 添加到网格 [Xamarin.Forms],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47240981/

相关文章:

ios - 将按钮添加到键盘工具栏

ios - 当应用程序在后台使用 Sinch iOS SDK 时无法处理来电推送通知

ios - CLLocation 没有给出实际速度

c# - 如何使应用程序适应 Windows 8 中的多种分辨率

c++ - Windows 通用应用程序 (XAML) : textBlock->Text cannot be called with the given argument list

c# - 从 .NET 迁移到 Java

c# - 声明成员变量并初始化而不重复类型

xaml - 在 Windows 8 Metro 应用程序中将超链接绑定(bind)到 richtextblock

c# - async 关键字和 TaskScheduler 的选择

c# - 什么是 "Sync Block"以及减少计数的技巧