A Microsoft platform for building and publishing apps for Windows devices.
Hello,
Welcome to Microsoft Q&A!
Displaying images in gridview using incremental loading
Please try to call LoadMoreItemsAsync manually like the following. And it's better that copy file to local foder before GridView load, the following is complete code.
public class ItemsToShow : ObservableCollection<Book>, ISupportIncrementalLoading
{
public int lastItem = 1;
public bool HasMoreItems
{
get
{
if (lastItem == 1000)
{
return false;
}
else
{
return true;
}
}
}
IReadOnlyList<StorageFile> files;
IReadOnlyList<StorageFile> thumbfiles;
StorageFolder komikthumb;
StorageFolder komik;
public async Task CopyResource()
{
await Task.Run(async () =>
{
StorageFolder localfolder = ApplicationData.Current.LocalFolder;
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);
komik = await library.CreateFolderAsync("komik", CreationCollisionOption.OpenIfExists);
files = await komik.GetFilesAsync();
if (files.Count == 0)
{
foreach (var item in _pdffiles)
{
await item.CopyAsync(komik, item.Name, NameCollisionOption.ReplaceExisting);
}
}
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 ApplicationData.Current.LocalFolder.CreateFolderAsync("thumb", CreationCollisionOption.OpenIfExists);
komikthumb = await thumbfolder.CreateFolderAsync("komik", CreationCollisionOption.OpenIfExists);
thumbfiles = await komikthumb.GetFilesAsync();
if (thumbfiles.Count == 0)
{
foreach (var item in _coverfiles)
{
await item.CopyAsync(komikthumb, item.Name, NameCollisionOption.ReplaceExisting);
}
}
});
}
public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
ProgressRing progressRing = ((Window.Current.Content as Frame).Content as MainPage).loading;
CoreDispatcher coreDispatcher = Window.Current.Dispatcher;
return Task.Run<LoadMoreItemsResult>(async () =>
{
await coreDispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
progressRing.Visibility = Visibility.Visible;
progressRing.IsActive = true;
});
files = await komik.GetFilesAsync();
IEnumerable<Temp> sortingFiles = files.Select(x => new Temp { File = x }).ToList();
IEnumerable<StorageFile> sortedfiles = sortingFiles.OrderByDescending(x => x.LastModified).Select(x => x.File).ToList();
string filePath = "";
foreach (StorageFile file in sortedfiles)
{
Book book = new Book();
book.Name = file.DisplayName.ToString();
StorageFile thumbFile = await komikthumb.GetFileAsync(file.Name.ToString() + ".png");
string path = komikthumb.Path;
filePath = Path.Combine(path, file.Name.ToString() + ".png");
await coreDispatcher.RunAsync(CoreDispatcherPriority.Normal,
async () =>
{
BitmapImage bi = new BitmapImage();
bi.SetSource(await thumbFile.OpenAsync(FileAccessMode.Read));
book.Image = bi;
Add(book);
});
}
await coreDispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
progressRing.Visibility = Visibility.Collapsed;
progressRing.IsActive = false;
});
return new LoadMoreItemsResult() { Count = count };
}).AsAsyncOperation<LoadMoreItemsResult>();
}
public class Temp
{
public StorageFile File { get; set; }
public DateTime LastModified { get; set; }
public string Name { get; set; }
}
}
Usage
private async void KomikContent()
{
var items = new ItemsToShow();
await items.CopyResource();
komikGridView.ItemsSource = items;
await items.LoadMoreItemsAsync(0);
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.