Xamarin.Forms中GridView中的动态列表

时间:2017-08-01 14:35:19

标签: c# xaml layout xamarin.forms grid

我需要做一个GridView,它接收一个我不知道的列表并显示它。我怎么能用xamarin表格呢? 我需要像我想要的那样重复布局,就像ListView中的模板一样......在ListView中我知道怎么做但在网格中我没有。 有没有办法将ListView外观设置为Grid数据? 我不知道它会有多少行和列,因为它依赖于数据量。

我的代码背后是:

 [XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CategoriasView : ContentPage
{
    List<Categorias> lstCategorias;
    public CategoriasView()
    {

        InitializeComponent();

        CallingCategoriasAsync();

    }

    void eteste()
    {
        int row = 0;
        int column = 0;
        foreach (var item in lstCategorias)
        {
            gridteste.Children.Add(new Label { Text = item.nome }, row, column);
            if (column == 0)
            {
                column = 1;
            }
            else
            {
                column = 0;
                row++;
            }

        }
        var i = 0;
    }

    async Task CallingCategoriasAsync()
    {
        lstCategorias = await Webservice.GetCategoriasAsync("1242");
        eteste();

    }
}

}

我的XAML是:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="neoFly_Montana.Views.CategoriasView">
<ContentPage.Content>
    <StackLayout>

            <Grid x:Name="gridteste">
            </Grid>

    </StackLayout>
</ContentPage.Content>

当我对它进行重新打印时,后面的工作正在进行,但屏幕上没有显示任何内容...我认为我需要在代码中进行修改......

1 个答案:

答案 0 :(得分:0)

//创建动态标签

Label lbl_new = new Label();  
lbl_new.Text="LablelText";

//分配行和列的位置

Grid.SetRow(lbl_new , RowNo);
Grid.SetColumn(lbl_new , ColNo);

//将控件分配给网格

gridteste.Children.Add(lbl_new);