【问题标题】:xamarin grid layout not showing any dataxamarin 网格布局不显示任何数据
【发布时间】:2017-11-26 19:03:53
【问题描述】:

我在 ListView 中有一个 Grid,并希望在每个 listview 项中显示一个 2 列网格。

我不想在模型上修复它,因为我想让它尽可能通用,因为我的后台提供“dynmaci 数据结构”(json)

所以我认为在后面的代码中这样做会更容易

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="Analytics.Views.AnlDataPage">
</ContentPage>

很简单,因为魔术在幕后发生。

代码隐藏

public partial class AnlDataPage : ContentPage
{
    AnlDataModel _model;

    public AnlDataPage(WfDataCollection objectData)
    {
        InitializeComponent();
        BindingContext = _model = new AnlDataModel(objectData);
        Populate();
    }

    public void Populate()
    {

        var AnlDataTemplate = new DataTemplate(() =>
        {
            var grid = new Grid();
            grid.ColumnDefinitions = new ColumnDefinitionCollection
            {
                new ColumnDefinition {
                    Width = new GridLength(1, GridUnitType.Star)
                },
                new ColumnDefinition {
                    Width = new GridLength(1, GridUnitType.Star)
                }
            };



            if (_model.Columns != null)
            {
                int i = 0;
                foreach (WfDataColumn column in _model.Columns)
                {
                    grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });

                    Label columnLabel = new Label {
                        Text = _model.ObjectData.Translations[column.LangKey],
                    };
                    Label valueLabel = new Label
                    {
                        Text = "some value",
                    };
                    grid.Children.Add(columnLabel, 0, i);
                    grid.Children.Add(valueLabel, 1, i);

                    i++;
                }
            }                

            return new ViewCell { View = grid };
        });

        Content = new StackLayout
        {
            Margin = new Thickness(20),
            Children = {
            new ListView { ItemsSource = _model.Data, ItemTemplate = AnlDataTemplate, Margin = new Thickness(0, 20, 0, 0) }
            }
        };
    }
}

输出是显示列表视图,并且行高看起来适合数据。

但listItem中没有显示任何内容。

如果我省略了grid.Children.Add(item,left,top) 中的lefttop,那么标签都会显示出来,但会重叠。

【问题讨论】:

    标签: c# listview xamarin xamarin.ios xamarin.forms


    【解决方案1】:

    对于遇到相同问题的任何人。

    基本上。我最初的方法完全没问题。唯一的问题是,如果你有高度不均匀的行,你需要设置一个Xamarin.Forms.ListView.HasUnevenRows Property

    女巫和我最初预期的一样。

       Content = new StackLayout
        {
            Margin = new Thickness(20),
            Children = {
                new ListView {
                    HasUnevenRows = true,
                    ItemsSource = _model.Data,
                    ItemTemplate = AnlDataTemplate,
                    Margin = new Thickness(0, 20, 0, 0)
                }
            }
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 2013-08-02
      相关资源
      最近更新 更多