0

I have a gridview with incremental loading that displays book covers from a local folder.

XAML:

<GridView  
    x:Name="komikGridView" 
        DataFetchSize="18" 
        IncrementalLoadingTrigger="Edge" 
        IncrementalLoadingThreshold="1" 
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch">
        <GridView.Resources>
            <DataTemplate x:Key="DataTemplatekomikGridView">
                    <Grid
                            x:Name="komikGrid1"
                                Margin="5,5,0,0"
                                Width="145"
                                Height="255"
                                Background="White">
                                <Image
                                    x:Name="cover"
                                        Width="145"
                                        Height="210"
                                        VerticalAlignment="Top"
                                        Source="{Binding Image}"
                                        Stretch="Fill" />
            </Grid>
        </DataTemplate>
    </GridView.Resources>
    <GridView.ItemTemplate>
        <StaticResource ResourceKey="DataTemplatekomikGridView"/>
    </GridView.ItemTemplate>
</GridView>

Code:

    var booksource = new BookSource();
            var collection = new IncrementalLoadingCollection<BookSource, Book>(booksource, 18);
            try
            {
                await collection.LoadMoreItemsAsync(0);
            }
 komikGridView.ItemsSource = collection;

Books.cs:

public class Book
    {
        public string Name { get; set; }

        public string Judul { get; set; }

        public string Image { get; set; }
    }

    public class BookSource : IIncrementalSource<Book>
    {
        public List<Book> _books;

        public BookSource()
        {
            _books = new List<Book>();
        }

        IReadOnlyList<StorageFile> files;
        IReadOnlyList<StorageFile> thumbfiles;
        StorageFolder kategorithumb;
        StorageFolder kategori;
        StorageFolder localfolder = ApplicationData.Current.LocalFolder;

        public async Task<List<Book>> CopyResource()
        {
            await Task.Run(async () =>
            {
                StorageFolder _pdffolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                _pdffolder = await _pdffolder.GetFolderAsync("files");
                _pdffolder = await _pdffolder.GetFolderAsync("pdf");
                _pdffolder = await _pdffolder.GetFolderAsync("komik");
                IReadOnlyList<StorageFile> _pdffiles = await _pdffolder.GetFilesAsync();
                StorageFolder library = await localfolder.CreateFolderAsync("library", CreationCollisionOption.OpenIfExists);
                kategori = await library.CreateFolderAsync("komik", CreationCollisionOption.OpenIfExists);
                files = await kategori.GetFilesAsync();
                if (((App)(App.Current)).FolderName == "komik" && files.Count == 0)
                {
                    foreach (var item in _pdffiles)
                    {
                        await item.CopyAsync(kategori, item.Name, NameCollisionOption.ReplaceExisting);
                    }
                    files = await kategori.GetFilesAsync();
                }
                StorageFolder _thumbfolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                _thumbfolder = await _thumbfolder.GetFolderAsync("files");
                _thumbfolder = await _thumbfolder.GetFolderAsync("cover");
                _thumbfolder = await _thumbfolder.GetFolderAsync("komik");
                IReadOnlyList<StorageFile> _coverfiles = await _thumbfolder.GetFilesAsync(); //which returns List<StorageFile>
                StorageFolder thumbfolder = await localfolder.CreateFolderAsync("thumb", CreationCollisionOption.OpenIfExists);
                kategorithumb = await thumbfolder.CreateFolderAsync("komik", CreationCollisionOption.OpenIfExists);
                thumbfiles = await kategorithumb.GetFilesAsync();
                if (((App)(App.Current)).FolderName == "komik" && thumbfiles.Count == 0)
                {
                    foreach (var item in _coverfiles)
                    {
                        await item.CopyAsync(kategorithumb, item.Name, NameCollisionOption.ReplaceExisting);
                    }
                }
            });
            IEnumerable<Temp> sortingFiles = files.Select(x => new Temp { File = x }).ToList();
            IEnumerable<StorageFile> sortedfiles = sortingFiles.OrderByDescending(x => x.LastModified).Select(x => x.File).ToList();
            var books = new List<Book>();
            string filePath = "";
            foreach (StorageFile file in sortedfiles)
            {
                Book book = new Book();
                StorageFile thumbFile = await kategorithumb.GetFileAsync(file.Name.ToString() + ".png");
                string path = kategorithumb.Path;
                filePath = Path.Combine(path, file.Name.ToString() + ".png");
                book.Name = file.DisplayName.ToString();
                book.Image = thumbFile.Path;
                await Window.Current.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    books.Add(book);
                });
            }
            return books;
        }

        private ProgressRing progressRing = ((Window.Current.Content as Frame).Content as LibraryPage).loading;
        public async Task<IEnumerable<Book>> GetPagedItemsAsync(int pageIndex, int pageSize, CancellationToken cancellationToken = default(CancellationToken))
        {
            progressRing.Visibility = Visibility.Visible;
            progressRing.IsActive = true;
            if (_books.Count == 0)
            {
                foreach (var item in await CopyResource())
                {
                    _books.Add(item);
                }
            }
            var result = (from p in _books
                          select p).Skip(pageIndex * pageSize).Take(pageSize);
            await Task.Delay(1000);
            progressRing.Visibility = Visibility.Collapsed;
            progressRing.IsActive = false;
            return result;
        }
        public class Temp
        {
            public StorageFile File { get; set; }
            public string Name { get; set; }
        }

I want to display 18 book covers first in gridview. Then if the user scrolls, it will display the next 18 book covers, and so on until all the book covers are displayed in gridview. How to apply it?

Rose
  • 613
  • 4
  • 22
  • So what is the current behavior? And it looks like you are asking a same question again and again:https://stackoverflow.com/questions/72851549/displaying-images-in-gridview-using-incremental-loading – Ax1le Sep 06 '22 at 03:09
  • The previous question was applying incremental loading, while what I am asking now is paging on the gridview (when the user scrolls the gridview) – Rose Sep 06 '22 at 03:11
  • why not just control the itemssource? 18 items for the first time, when you scroll the view, add another 18 items – Ax1le Sep 06 '22 at 03:13
  • How to control the itemssource? – Rose Sep 06 '22 at 03:16
  • As I said, put 18 items to the itemssource that you assigned to the Gridview when you first load it. Then detect the scorll, add other 18 items to the source – Ax1le Sep 06 '22 at 03:45
  • You want to load 18 images at the beginning and then load more when the user scrolls and finally show all the images, right? I think the question is still an incremental Loading question. Nico's answer :https://stackoverflow.com/questions/72851549/displaying-images-in-gridview-using-incremental-loading should already answered your question. – Roy Li - MSFT Sep 06 '22 at 09:25
  • Yes, you are right, But that answer doesn't work to load 18 images at the beginning and then load more when user scrolls – Rose Sep 07 '22 at 02:13

1 Answers1

0

If you need to control the size of the source that is loaded every time. What you need is just set the itemsPerPage argument when you create the IncrementalLoadingCollection as 18.

Like this:

  collection = new IncrementalLoadingCollection<NamedColorSource, NamedColor>(18);

Then you could put a break point in the GetPagedItemsAsync method and check the pageSize parameter. You will see it is 18 as you want.

Roy Li - MSFT
  • 8,043
  • 1
  • 7
  • 13