C# Xamarin表单ViewCell不显示与列表绑定的数据,但与元组一起使用

C# Xamarin表单ViewCell不显示与列表绑定的数据,但与元组一起使用,c#,listview,data-binding,xamarin.forms,binding-context,C#,Listview,Data Binding,Xamarin.forms,Binding Context,当我尝试在Xamarin表单中连接viewcell以显示BindingContext中的数据时,会出现奇怪的行为 基本上,我填充一个对象来保存3张图像(除其他外)-我是Xamarin的新手,我想我会尝试建立一个instagram风格的照片库,在这里,我每行有3张可点击的图像,并使UI简洁、响应迅速,没有延迟和内存问题。正在探索使用listview执行此操作,这样我就可以获得viewcell重用功能 无论如何,我通过BindingContext将此对象传递给自定义视图单元。使用元组时,图像显示了

当我尝试在Xamarin表单中连接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);
公共类ExploreVCell:ViewCell
{
公共单元格()
{
Image image1=新图像();
Image image2=新图像();
Image image3=新图像();
//名单;
标签lbl=新标签();
网格=新网格
{
衬垫=新厚度(10),
RowDefinitions={new RowDefinition{Height=GridLength.Star}},
列定义={
新列定义{Width=GridLength.Star},
新列定义{Width=GridLength.Star},
新列定义{Width=GridLength.Star}
},
BackgroundColor=Color.Blue
};
grid.Children.Add(image1,0,0);
grid.Children.Add(图像2,1,0);
grid.Children.Add(图像3,2,0);
视图=网格;
image1.SetBinding(Image.SourceProperty,i=>i.MyTuple.Item1.ImageUrl);
image2.SetBinding(Image.SourceProperty,i=>i.MyTuple.Item2.ImageUrl);
image3.SetBinding(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);
//这些行不起作用-奇怪
image1.SetBinding(Image.SourceProperty,i=>i.RowData[0].ImageUrl);
image2.SetBinding(Image.SourceProperty,i=>i.RowData[1].ImageUrl);
image3.SetBinding(Image.SourceProperty,i=>i.RowData[2].ImageUrl);
这是在我将数据填充到viewcell后绑定到viewcell的类。正如您所看到的,我使用的是ObservableCollection,但它与列表的行为相同…只有元组似乎可以工作。现在还可以,但如果我需要使用枚举方法,该怎么办:(

公共类ListViewRow
{
公共ListViewRow(StylePost p、StylePost q、StylePost r)
{
MyTuple=新的Tuple(p,q,r);
RowData=新的ObservableCollection();
RowData.Add(p);
RowData.Add(q);
RowData.Add(r);
}
公共元组MyTuple{get;set;}
公共ObservableCollection行数据{get;set;}
}

看起来表单中可能存在漏洞,我需要进一步调查,但您的图片被收藏的原因是什么?希望下载图片并高效地在可滚动的网格中显示它们。大多数照片库网格看起来像instagram和google照片一样有3张拇指尾图片宽。我想我应该打包我的一个长长的收藏nto是一个3幅图像的集合,即每行3幅。如果我能做到这一点,我将设法找到一种方法,在每张图像上添加一个点击手势,而不影响listview的点击动作,从而允许我从初始网格类型视图将每张图像打开到它自己的详细页面。我会使用网格,但担心在滚动页面上有100个缩略图是否要删除内存,即不可回收?hmm请打开建议,以不同于我的方式显示图像网格,如instagram:)如果始终为3,则可以为column1 image、Column2 image和Column3 image设置单独的属性。理想情况下,WPF中的WrapPanel之类的东西可以很好地实现这一点,但我不知道在Xamarin表单中,如果创建自定义容器,如何实现列表和行循环的虚拟化。看看FlowListView和FFImageLoading
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; }

}