我有一个具有增量加载功能的网格视图,可以显示本地文件夹中的书籍封面。
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>
代码:
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; }
}
我想在gridview中首先显示18本书的封面。然后如果用户滚动,它将显示接下来的 18 本书封面,依此类推,直到所有书籍封面都显示在 gridview 中。如何应用?
如果需要控制每次加载的源的大小。您需要的只是在创建
itemsPerPage
时将 IncrementalLoadingCollection
参数设置为 18。
像这样:
collection = new IncrementalLoadingCollection<NamedColorSource, NamedColor>(18);
然后您可以在
GetPagedItemsAsync
方法中放置一个断点并检查 pageSize
参数。你会看到它是你想要的 18。