【问题标题】:a dynamic list in a GridView in Xamarin.FormsXamarin.Forms 中 GridView 中的动态列表
【发布时间】:2018-01-08 11:53:16
【问题描述】:

我需要做一个 GridView 来接收我不知道的列表并显示它。我怎样才能以 xamarin 形式做到这一点? 我需要像我想要的那样重复布局,就像 ListView 中的模板...在 ListView 中我知道如何做到这一点,但在 Grid 中我不知道。 没有办法将 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>

当我调试它时,后面正在工作,但屏幕上没有显示任何内容...我想我需要在代码中添加一些东西...

【问题讨论】:

  • Grid 和 ListView 是两种截然不同的视图,你知道的,对吧? Grid 是一个布局,而 listview 是一个 ui 组件......你能更好地解释一下你的场景吗?
  • 是的,我知道...但是,如果我确定,我可以对 RecycleView 说,如果我希望它是一个网格或一个列表。我的情况是:我将收到一个我不知道它有多少项目的数据列表,我需要将它设置在一个网格中,并为每个项目定义一个布局。
  • 我明白了。我不知道你说的是 RecyclerView。所以可能会有这样的人 -> developer.xamarin.com/guides/android/user_interface/…
  • 听起来您需要以编程方式填充 GridView:developer.xamarin.com/guides/xamarin-forms/user-interface/…
  • @JoycedeLanna 你没有。您只需要添加像myGrid.Children.Add(myLabel, row, col); myGrid.Children.Add(myButton, row, col + 1); 这样的视图...您还可以使用liestview xaml 中的网格定义模板并摆脱所有这些冗长

标签: c# xaml layout xamarin.forms grid


【解决方案1】:

//创建动态标签

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

//分配行列位置

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

//将控件分配给网格

gridteste.Children.Add(lbl_new);

【讨论】:

    猜你喜欢
    • 2019-09-02
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多