C# 增量加载和捕捉的视觉状态
我的应用程序页面中有一个listview(用于快照状态)和一个gridview(用于正常状态)(这是一个项目页面模板),它们都绑定到实现C# 增量加载和捕捉的视觉状态,c#,xaml,listview,gridview,windows-store-apps,C#,Xaml,Listview,Gridview,Windows Store Apps,我的应用程序页面中有一个listview(用于快照状态)和一个gridview(用于正常状态)(这是一个项目页面模板),它们都绑定到实现ISupportIncrementalLoading接口的集合。以下是实现方法: public HasMoreItemsDelegate MoreItemsExist { get; set; } public LoadMoreItemsDelegate<T> ItemsLoadAsync { get; set; }
ISupportIncrementalLoading
接口的集合。以下是实现方法:
public HasMoreItemsDelegate MoreItemsExist { get; set; }
public LoadMoreItemsDelegate<T> ItemsLoadAsync { get; set; }
public bool HasMoreItems
{
get
{
return this.MoreItemsExist();
}
}
private bool isLoading;
public bool IsLoading
{
get
{
return this.isLoading;
}
private set
{
this.isLoading = value;
this.OnPropertyChanged("IsLoading");
}
}
public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
if (this.IsLoading)
{
throw new InvalidOperationException("Only one operation in flight at a time");
}
this.IsLoading = true;
return AsyncInfo.Run((c) => LoadMoreItemsAsync(c, count));
}
async Task<LoadMoreItemsResult> LoadMoreItemsAsync(CancellationToken c, uint count)
{
try
{
IEnumerable<T> itemsToAdd = await ItemsLoadAsync(c, count);
foreach (var item in itemsToAdd)
{
this.Add(item);
}
return new LoadMoreItemsResult { Count = (uint)itemsToAdd.Count() };
}
finally
{
this.IsLoading = false;
}
}
public HasMoreItemsDelegate MoreItemsExist{get;set;}
public LoadMoreItemsDelegate ItemsLoadAsync{get;set;}
公共物品
{
得到
{
返回此.MoreItemsExist();
}
}
私有布尔孤岛加载;
公共布尔孤岛加载
{
得到
{
返回此。正在加载;
}
专用设备
{
this.isLoading=值;
此.OnPropertyChanged(“IsLoading”);
}
}
公众Windows.Foundation.IAsyncOperation LoadMoreItemsAsync(尤金特)
{
如果(此。正在加载)
{
抛出新的InvalidOperationException(“一次只运行一个操作”);
}
this.IsLoading=true;
返回AsyncInfo.Run((c)=>LoadMoreItemsAsync(c,count));
}
异步任务LoadMoreItemsAsync(CancellationToken c,uint计数)
{
尝试
{
IEnumerable itemsToAdd=wait ItemsLoadAsync(c,count);
foreach(itemsToAdd中的var项)
{
本条增加(项目);
}
返回新的LoadMoreItemsResult{Count=(uint)itemsToAdd.Count()};
}
最后
{
this.IsLoading=false;
}
}
我遇到的问题是:当应用程序处于快照模式,我使用listview和gridview导航到页面时,两个视图都尝试调用
LoadMoreItemsAsync
,我输入exception子句,而在正常(全屏)模式下,只有gridview调用该方法,而listview没有。问题来自于模板的工作方式。默认情况下,gridview可见,而listview不可见。当我们导航到该页面时,在呈现页面后调用VisualStateManager
,因此当导航到该页面时,gridview会在短时间内可见,并在隐藏之前调用LoadMoreItemsAsync
方法。情节提要完成后,将显示listview,并调用LoadMoreItemsAsync
,然后出现问题
解决方法:使这两个视图在默认情况下折叠,并在显示在
VisualStateManager
中时进行处理(它总是在导航到页面后调用)。问题来自模板的工作方式。默认情况下,gridview可见,而listview不可见。当我们导航到该页面时,在呈现页面后调用VisualStateManager
,因此当导航到该页面时,gridview会在短时间内可见,并在隐藏之前调用LoadMoreItemsAsync
方法。情节提要完成后,将显示listview,并调用LoadMoreItemsAsync
,然后出现问题
解决方法:使这两个视图在默认情况下折叠,并在显示在VisualStateManager
中时进行处理(导航到页面后总是调用它)