gpt4 book ai didi

c# - LoadMoreItemsAsync 调用可变时间

转载 作者:太空宇宙 更新时间:2023-11-03 15:47:10 26 4
gpt4 key购买 nike

我一直在尝试为 ObservableCollection 实现 ISupportIncrementalLoading 接口(interface)。我的实现大部分都有效,但在不确定的情况下会表现得很奇怪。这是我的 IncrementalCollection 类代码。

public class IncrementalCollection<T> : ObservableCollection<T>, ISupportIncrementalLoading
{
private bool hasMoreItems;
private int currentPage;
private string filter;

private Func<string, int, Task<IList<T>>> func;
Action onLoadingStarts;
Action onLoadingEnds;

public IncrementalCollection(Func<string, int, Task<IList<T>>> func, Action onLoadingStarts, Action onLoadingEnds)
{
this.func = func;
this.hasMoreItems = true;

this.onLoadingStarts = onLoadingStarts;
this.onLoadingEnds = onLoadingEnds;
}

public void ResetCollection(string filter)
{
currentPage = 0;
this.filter = filter;
this.Clear();
}

public bool HasMoreItems
{
get { return hasMoreItems; }
}

public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
return DoLoadMoreItemsAsync(count).AsAsyncOperation<LoadMoreItemsResult>();
}

private async Task<LoadMoreItemsResult> DoLoadMoreItemsAsync(uint count)
{
onLoadingStarts();
var result = await func(this.filter, ++this.currentPage);
if (result == null || result.Count == 0)
{
hasMoreItems = false;
}
else
{
foreach (T item in result)
this.Add(item);
}

onLoadingEnds();
return new LoadMoreItemsResult() { Count = result == null ? 0 : (uint)result.Count };
}
}

第一个奇怪的行为发生在页面加载时,LoadMoreItemsAsync 函数有时被调用一次,一般是两次,有时甚至超过两次。这很奇怪,因为一次调用就足以将足够多的项目添加到集合中。我什至尝试提取更多数据(2-3 次),但这种行为仍在继续。 IncrementalCollection 对象的初始化位置可能有问题。似乎加载页面所需的时间越长,对 LoadMoreItemsAsync 函数的调用就越多。我正在像这样在 NavigationHelper_LoadState 函数中创建集合。

_users = new IncrementalCollection<User>((filter, page) => _dataService.GetUserList(url, filter, null, page), onLoadingStarts, onLoadingEnds);

第二个奇怪的行为是关于缓存的,虽然我已经添加了

this.NavigationCacheMode = NavigationCacheMode.Disabled;

到每个页面构造函数,还更改了 NavigationHelper 以在返回导航时不保存 pageState。感觉网络请求被缓存了,因为在这段时间内很难返回响应。

public void OnNavigatedFrom(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
return;

var frameState = SuspensionManager.SessionStateForFrame(this.Frame);
var pageState = new Dictionary<String, Object>();
if (this.SaveState != null)
{
this.SaveState(this, new SaveStateEventArgs(pageState));
}
frameState[_pageKey] = pageState;
}

感谢任何有关这些奇怪行为的帮助。

还有关于 ISupportIncrementalLoading 接口(interface)的任何很好的教程,它解释了 LoadMoreItemsAsync 触发条件。我正在尝试修改 WrapPanel 实现,但不知道从哪里开始,因为我不知道它在寻找什么。这可能与 ItemHeight 有关,但还是具体信息更好。

提前致谢。

最佳答案

ISupportIncrementalLoading 接口(interface)中似乎存在错误。通过在此处应用解决方案解决了多个请求问题 Create a ListView with LoadMoreItemsAsync on end of scroll .

我将 foreach 循环包装在 Task.WhenAll 调用中。

await Task.WhenAll(Task.Delay(50), Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
foreach (T item in result)
this.Add(item);
}).AsTask());

关于c# - LoadMoreItemsAsync 调用可变时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27676607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com