C# 在集合视图中增量加载数据
我正在尝试以增量方式加载数据,但RemainingItemsThreshold未触发RemainingItemsThreshold reached命令 这里是与此相关的文档: : 这里是我的收藏查看页面:C# 在集合视图中增量加载数据,c#,xamarin,xamarin.forms,xamarin.android,C#,Xamarin,Xamarin.forms,Xamarin.android,我正在尝试以增量方式加载数据,但RemainingItemsThreshold未触发RemainingItemsThreshold reached命令 这里是与此相关的文档: : 这里是我的收藏查看页面: <ContentPage.Content> <StackLayout BackgroundColor="#b4b4b4"> <RefreshView Command="{Binding RefreshItemsCommand}" IsRef
<ContentPage.Content>
<StackLayout BackgroundColor="#b4b4b4">
<RefreshView Command="{Binding RefreshItemsCommand}" IsRefreshing="{Binding IsRefreshing}" >
<CollectionView x:Name="CollectionViewThumbnails" SelectionMode="Multiple" Margin="13" ItemsSource="{Binding Items}"
RemainingItemsThresholdReachedCommand="{Binding ItemTresholdReachedCommand}"
RemainingItemsThreshold="{Binding ItemTreshold}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="1" VerticalItemSpacing="13" HorizontalItemSpacing="13"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate x:Name="data">
<Frame >
<StackLayout>
<Image Source="{Binding EmplacementThumbnails}" Aspect="AspectFill"/>
</StackLayout>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
<ActivityIndicator IsRunning="{Binding IsBusy}"
HeightRequest="30"
HorizontalOptions="Center"
VerticalOptions="Center"
WidthRequest="30"/>
<StackLayout>
<Button Text="Go" Clicked="StartProcessConcatePages"/>
</StackLayout>
</StackLayout>
</ContentPage.Content>
我的代码隐藏:
public partial class ConcatePageThumbnails : ContentPage
{
FileEndpoint fileEndpoint = new FileEndpoint();
FileInfo fileInfo;
BitmapDataStore viewModel;
protected async override void OnAppearing()
{
base.OnAppearing();
if (viewModel.Items.Count == 0)
viewModel.LoadItemsCommand.Execute(null);
MessagingCenter.Subscribe<object, ThumbnailsModel>(this, BitmapDataStore.ScrollToPreviousLastItem, (sender, item) =>
{
CollectionViewThumbnails.ScrollTo(item, ScrollToPosition.End);
});
}
//TODO -- Gerer le retour
public ConcatePageThumbnails(FileInfo fileInfo)
{
InitializeComponent();
this.fileInfo = fileInfo;
BindingContext = viewModel = new BitmapDataStore(fileInfo);
}
}
public分部类concatepage缩略图:ContentPage
{
FileEndpoint FileEndpoint=新的FileEndpoint();
FileInfo FileInfo;
位图数据存储视图模型;
受保护的异步重写void OnAppearing()
{
base.OnAppearing();
if(viewModel.Items.Count==0)
viewModel.LoadItemsCommand.Execute(null);
MessagingCenter.Subscribe(此,BitmapDataStore.ScrollTopPreviousLastItem,(发件人,项目)=>
{
CollectionViewThumbnails.ScrollTo(项目,ScrollToPosition.End);
});
}
//待办事项——格雷尔·勒雷图尔
public ConcatePageThumbnails(文件信息文件信息)
{
初始化组件();
this.fileInfo=fileInfo;
BindingContext=viewModel=new BitmapDataStore(fileInfo);
}
}
和我的视图模型:
public class BitmapDataStore
{
IGetThumbnails getThumbnails;
FileInfo fileInfo;
public List<ThumbnailsModel> Items { get; set; }
public Command LoadItemsCommand { get; set; }
public Command ItemTresholdReachedCommand { get; set; }
public Command RefreshItemsCommand { get; set; }
public const string ScrollToPreviousLastItem = "Scroll_ToPrevious";
private int _itemTreshold;
private bool _isRefreshing;
bool isBusy = false;
public bool IsBusy
{
get { return isBusy; }
set { isBusy = value; }
}
public bool IsRefreshing
{
get { return _isRefreshing; }
set { _isRefreshing = value; }
}
public int ItemTreshold
{
get { return _itemTreshold; }
set { _itemTreshold = value; }
}
public BitmapDataStore(FileInfo fileInfo)
{
ItemTreshold = 2;
Items = new List<ThumbnailsModel>();
this.fileInfo = fileInfo;
getThumbnails = DependencyService.Get<IGetThumbnails>();
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());
ItemTresholdReachedCommand = new Command(async () => await ItemsTresholdReached());
RefreshItemsCommand = new Command(async () =>
{
await ExecuteLoadItemsCommand();
IsRefreshing = false;
});
}
async Task ItemsTresholdReached()
{
if (IsBusy)
return;
IsBusy = true;
try
{
var items = await getThumbnails.GetBitmaps(fileInfo.FullName, Items.Count);
var previousLastItem = Items.Last();
foreach (var item in items)
{
Items.Add(item);
}
Debug.WriteLine($"{items.Count()} {Items.Count} ");
if (items.Count() == 0)
{
ItemTreshold = -1;
return;
}
MessagingCenter.Send<object, ThumbnailsModel>(this, ScrollToPreviousLastItem, previousLastItem);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
async Task ExecuteLoadItemsCommand()
{
if (IsBusy)
return;
IsBusy = true;
try
{
ItemTreshold = 2;
Items.Clear();
var items = await getThumbnails.GetBitmaps(fileInfo.FullName, Items.Count);
foreach (var item in items)
{
Items.Add(item);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
}
公共类位图数据存储
{
IGetThumbnails获取缩略图;
FileInfo FileInfo;
公共列表项{get;set;}
公共命令LoadItemsCommand{get;set;}
公共命令项TRESHOLDREACHEDCOMAND{get;set;}
公共命令RefreshItemsCommand{get;set;}
公共常量字符串ScrollTopPreviousLastItem=“Scroll\u TopPrevious”;
私人国际项目;
私人住宅正在翻新;
bool isBusy=false;
公共图书馆很忙
{
获取{return isBusy;}
设置{isBusy=value;}
}
公共图书馆正在刷新
{
获取{return\u isRefreshing;}
设置{u isRefreshing=value;}
}
公共int ItemTreshold
{
获取{return\u itemTreshold;}
设置{u itemTreshold=value;}
}
公共位图数据存储(FileInfo FileInfo)
{
ItemTreshold=2;
项目=新列表();
this.fileInfo=fileInfo;
getThumbnails=DependencyService.Get();
LoadItemsCommand=new命令(async()=>await ExecuteLoadItemsCommand());
ItemTresholdReachedCommand=新命令(异步()=>等待itemsresholdreached());
RefreshItemsCommand=新命令(异步()=>
{
等待ExecuteLoadItemsCommand();
IsRefreshing=假;
});
}
异步任务项StresholdReach()
{
如果(忙)
返回;
IsBusy=true;
尝试
{
var items=await getThumbnails.GetBitmaps(fileInfo.FullName,items.Count);
var previousLastItem=Items.Last();
foreach(项目中的var项目)
{
项目。添加(项目);
}
Debug.WriteLine($“{items.Count()}{items.Count}”);
如果(items.Count()==0)
{
ItemTreshold=-1;
返回;
}
MessagingCenter.Send(此,滚动到上一个LastItem,上一个LastItem);
}
捕获(例外情况除外)
{
Debug.WriteLine(ex);
}
最后
{
IsBusy=false;
}
}
异步任务ExecuteLoadItemsCommand()
{
如果(忙)
返回;
IsBusy=true;
尝试
{
ItemTreshold=2;
Items.Clear();
var items=await getThumbnails.GetBitmaps(fileInfo.FullName,items.Count);
foreach(项目中的var项目)
{
项目。添加(项目);
}
}
捕获(例外情况除外)
{
Debug.WriteLine(ex);
}
最后
{
IsBusy=false;
}
}
}
我的Dependencyservice类,从中可以获取收藏视图的缩略图和项目:
public async Task<List<ThumbnailsModel>> GetBitmaps(string filePath, int lastIndex = 0)
{
var sw = new Stopwatch();
sw.Start();
int numberOfItemsPerPage = 6;
PdfRenderer pdfRenderer = new PdfRenderer(GetSeekableFileDescriptor(filePath));
var appDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
string fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
string directoryPath = System.IO.Path.Combine(appDirectory, "thumbnailsTemp", System.IO.Path.GetFileNameWithoutExtension(fileName));
List<ThumbnailsModel> thumbnailsModels = new List<ThumbnailsModel>();
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
//int pageCount = pdfRenderer.PageCount;
for (int i = lastIndex; i < lastIndex + numberOfItemsPerPage; i++)
{
PdfRenderer.Page page = pdfRenderer.OpenPage(i);
Android.Graphics.Bitmap bmp = Android.Graphics.Bitmap.CreateBitmap(page.Width, page.Height, Android.Graphics.Bitmap.Config.Argb4444);
page.Render(bmp, null, null, PdfRenderMode.ForDisplay);
try
{
using (FileStream output = new FileStream(System.IO.Path.Combine(directoryPath, fileName + "Thumbnails" + i + ".png"), FileMode.Create))
{
bmp.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 0, output);
}
page.Close();
}
catch (Exception ex)
{
//TODO -- GERER CETTE EXPEXPTION
throw new Exception();
}
}
int y = 1;
Directory.GetFiles(directoryPath).ToList<string>().Skip(lastIndex).Take(numberOfItemsPerPage).ForEach(delegate (string thumbnailsEmplacement)
{
thumbnailsModels.Add(new ThumbnailsModel(y, thumbnailsEmplacement));
y++;
});
sw.Stop();
return await Task.FromResult(thumbnailsModels);
}
公共异步任务GetBitmaps(字符串文件路径,int lastIndex=0)
{
var sw=新秒表();
sw.Start();
int numberOfItemsPerPage=6;
PdfRenderer PdfRenderer=新的PdfRenderer(GetSeekableFileDescriptor(filePath));
var appDirectory=System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
字符串fileName=System.IO.Path.GetFileNameWithoutExtension(filePath);
string directoryPath=System.IO.Path.Combine(appDirectory,“thumbnailsTemp”,System.IO.Path.GetFileNameWithoutExtension(fileName));
List thumbnailsModels=新建列表();
如果(!Directory.Exists(directoryPath))
{
CreateDirectory(directoryPath);
}
//int pageCount=pdfRenderer.pageCount;
for(int i=lastIndex;i