ListView仅在出现时加载项目

时间:2019-06-26 10:59:19

标签: c# listview xamarin.forms

我有一个ListViewViewCell一起展示一些手工制作的Image。问题是Image仅在出现ViewCell时显示。 因此,在滚动时,我可以使单元格没有图像,即使该单元格已经显示一次,图像也会在之后显示。

是否有一种方法可以强制ListView始终显示所有内容,即使它没有出现?

一些代码: C#

ObservableCollection<MyCell> ListCells = new ObservableCollection<MyCellView>();
myListView.ItemTemplate = new DataTemplate(typeof(MyCellView));
myListView.HasUnevenRows = true;
myListView.SeparatorVisibility = SeparatorVisibility.None;
myListView.RefreshControlColor = Color.Transparent;

然后将项目添加到列表中。

MyCells cell = new MyCells()
{
    ImagePath = myPath
};
ListCells.Add(cell);

MyCells类:

public class MyCells : INotifyPropertyChanged
{
    private string imagePath;
    public string ImagePath
    { 
        get => imagePath;
        set
        {
            imagePath = value;
            OnPropertyChanged("ImagePath");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

MyCellView类

public class MyCellView : ViewCell
{
    public MyCellView()
    {
        Grid grid = new Grid
        {
            Margin = new Thickness(20)
        };

        grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(200) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new gridLength(1, GridUnitType.Star) });

        Image img = new Image();
        img.SetBinding(Image.SourceProperty,"ImagePath");

        grid.Children.Add(img);

        View = grid;
    }
}

0 个答案:

没有答案