WP 8.1 ISupportIncrementalLoading LoadMoreItemsAsync不断被无休止地调用

我试图使用以下示例实现无限滚动

http://www.davidbritch.com/2014/05/data-virtualisation-using.html

问题是在我的情况下, LoadMoreItemsAsync不断被无休止地调用 。 我正在集线器上开发这个(不确定这是否有所不同)并使用MVVMLight。 以下是我的代码

的.xaml

                   

下面给出了我对ISupportIncrementalLoading的实现

 public class IncrementalLoadingNotificationsCollection : ObservableCollection, ISupportIncrementalLoading { private INotificationService _notificationService; public IncrementalLoadingNotificationsCollection(INotificationService notificationService) { HasMoreItems = true; _notificationService = notificationService; } public bool HasMoreItems { get; private set; } public IAsyncOperation LoadMoreItemsAsync(uint count) { return InnerLoadMoreItemsAsync(count).AsAsyncOperation(); } private async Task InnerLoadMoreItemsAsync(uint expectedCount) { var actualCount = 0; IList notifications; try { notifications = await _notificationService.GetNotificationsAsync(ConfigurationSettings.AccessToken, 8); } catch (Exception) { HasMoreItems = false; throw; } if (notifications != null && notifications.Any()) { foreach (var notification in notifications) { Add(notification); } actualCount += notifications.Count; //_photoStartIndex += (uint)actualCount; } else { HasMoreItems = false; } return new LoadMoreItemsResult { Count = (uint)actualCount }; } } 

以下是viewmodel的摘录

 public IncrementalLoadingNotificationsCollection IncrementalNotifications { get { return _incrementalNotifications; } set { _incrementalNotifications = value; if (!Equals(null) && _incrementalNotifications.Count > 0) { DispatcherHelper.CheckBeginInvokeOnUI(() => { RaisePropertyChanged(() => IncrementalNotifications); }); } } } 

任何帮助解决这个问题都非常感谢。

正如您在答案中提到的,StackPanel是罪魁祸首。 StackPanel控件不会限制其内容的大小。 这意味着当ListView加载更多项时,ListView的高度也会在StackPanel内增长,因此ListView认为每个项都是可见的,因此它会加载更多项,依此类推。

这也会影响虚拟化,这也是你永远不应该将ListView放在StackPanel中的另一个原因。 根据文件 :

如果ItemsControl的视口大小不受限制,则控件不会执行虚拟化。 相反,它为其集合中的每个项目创建一个项容器。 一些不限制视口大小的常见容器是Canvas,StackPanel和ScrollViewer。 您可以通过直接设置ItemsControl的大小来启用此情况下的虚拟化,而不是通过其父容器resize。

所以你的选择是:

  • 不要使用StackPanel容器。 改为使用网格。
  • 如果要使用StackPanel容器,则必须手动设置ListView的大小(高度)。

玩完之后我设法解决了这个问题。 问题是ListView所在的StackPanel。我不知道为什么但是由于某种原因,StackPanel的存在导致LoadMoreItemsAsync被无休止地调用,并且一旦我删除它,它就可以正常工作。