Hello,
I have a list view with many items, I wanted to load my list view items with a default image or load the first items displayed on the screen.
Then depending the scroll and the items visible on the screen, I would like to update each items visible with the related image stream.
I tried to made a quick sample but it doesn't work at all, my app is crashing : "Sharing violation on path ...." ,when I the scrolling event is raised.
Any idea to make this auto-updating listView depending the visibility of each items on the UI ?
public partial class MyClass : ContentPage
{
private readonly ThumbnailsViewModel thumbnailsViewModel;
private readonly FileInfo fileInfo;
IList list = new List();
public MyClass (FileInfo fileInfo)
{
InitializeComponent();
this.fileInfo = fileInfo;
this.thumbnailsViewModel = new ThumbnailsViewModel(fileInfo);
VisualContainer visualContainer;
visualContainer = ThumbnailsListView.GetVisualContainer();
int numberOfPage = DependencyService.Get().GetAllPages(fileInfo.FullName);
int i = 0;
while (i < numberOfPage)
{
list.Add(new model { image = ImageSource.FromFile("award.png") });
i++;
}
ThumbnailsListView.ItemsSource = list;
var scrollview = ThumbnailsListView.GetScrollView();
scrollview.Scrolled += (sender, e) => Scrollview_Scrolled(sender, e, visualContainer);
}
private void Scrollview_Scrolled(object sender, ScrolledEventArgs e, VisualContainer vs)
{
VisibleLinesCollection visibleLineInfos = vs.ScrollRows.GetVisibleLines();
var first = visibleLineInfos.First();
int firstIndex = first.LineIndex;
int lastItemVisble = visibleLineInfos.Count;
int i = firstIndex;
while (i <= lastItemVisble)
{
MemoryStream stream = PdfToImage.FileToImage(fileInfo, i);
byte[] files = stream.ToArray();
var m = new model { image = ImageSource.FromStream(() => new MemoryStream(files)) };
list[i] = m;
i++;
}
}
}
public class model
{
public ImageSource image { get; set; }
}