0

当我尝试在 Xamarin Forms 中连接 viewcell 以显示来自 BindingContext 的数据时出现奇怪的行为。

基本上我填充了一个对象来保存 3 张图片(除了其他东西) - 我是 Xamarin 的新手,我想我会尝试弄清楚如何构建一个 instagram 风格的照片库,我每行有 3 个可点击的图像并制作UI 快速且响应迅速,没有延迟和内存问题。探索使用 listview 进行此操作,以便我可以获得 viewcell 重用功能??...

在此处输入图像描述

无论如何,我通过 BindingContext 将此对象传递给自定义视图单元格。使用元组时,图像显示了一些东西。但是,如果我使用列表项,则不会。

我不明白为什么它会显示一个而不显示另一个 - 元组和列表同时填充并保存信息?

public class ExploreVCell : ViewCell
{
    public ExploreVCell()
    {
        Image image1 = new Image();
        Image image2 = new Image();
        Image image3 = new Image();

       // List<StylePost> list;

        Label lbl = new Label ();

        Grid grid = new Grid
        {
            Padding = new Thickness(10),
            RowDefinitions = { new RowDefinition { Height = GridLength.Star } },
            ColumnDefinitions = {
                new ColumnDefinition { Width = GridLength.Star },
                new ColumnDefinition { Width = GridLength.Star },
                new ColumnDefinition { Width = GridLength.Star }
            },
            BackgroundColor=Color.Blue
        };

        grid.Children.Add(image1, 0, 0);
        grid.Children.Add(image2, 1, 0);
        grid.Children.Add(image3, 2, 0);

        View = grid;

        image1.SetBinding<ListViewRow>(Image.SourceProperty, i => i.MyTuple.Item1.ImageUrl);
        image2.SetBinding<ListViewRow>(Image.SourceProperty, i => i.MyTuple.Item2.ImageUrl);
        image3.SetBinding<ListViewRow>(Image.SourceProperty, i => i.MyTuple.Item3.ImageUrl);

上述方法有效,但当我将图像绑定换成通过以下设置时无效:

        // doesnt work with these lines - weird
        image1.SetBinding<ListViewRow>(Image.SourceProperty, i => i.RowData[0].ImageUrl);
        image2.SetBinding<ListViewRow>(Image.SourceProperty, i => i.RowData[1].ImageUrl);
        image3.SetBinding<ListViewRow>(Image.SourceProperty, i => i.RowData[2].ImageUrl);

这是在我将数据填充到视单元后绑定到视单元的类。如您所见,我使用的是 ObservableCollection,但它与 List 的行为相同......似乎只有一个元组有效。现在很好,但是如果我需要使用枚举方法怎么办:(

public class ListViewRow
{
    public ListViewRow(StylePost p, StylePost q, StylePost r)
    {
        MyTuple = new Tuple<StylePost, StylePost, StylePost>(p, q, r);
        RowData = new ObservableCollection<StylePost>();
        RowData.Add(p);
        RowData.Add(q);
        RowData.Add(r);

    }
    public Tuple<StylePost, StylePost, StylePost> MyTuple { get; set; }
    public ObservableCollection<StylePost> RowData { get; set; }

}
4

0 回答 0