WP 8.1的ISupportIncrementalLoading的LoadMoreItemsAsync函数一直被无限调用

4
我正在尝试使用以下示例实现无限滚动:

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

问题在于,在我的情况下,LoadMoreItemsAsync一直被无限调用。我是在hub上开发的(不确定这是否有影响),并使用MVVMLight。以下是我的代码:

.xaml

<Page
x:Class="MyFileServer.UniversalApp.AppHubPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyFileServer.UniversalApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding Source={StaticResource MFSViewModelLocator}, Path=AppHub}">

<Grid>
    <Hub Header="My File Server">
        <HubSection x:Name="MFSNotifications" Header="Notifications">
            <DataTemplate>
                <StackPanel>
                    <ListView x:Name="Notifications"  ItemsSource="{Binding IncrementalNotifications}" >
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding NotificationDescription}"/>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackPanel>
            </DataTemplate>
        </HubSection>
        <HubSection x:Name="MFSFiles" Header="Files"></HubSection>
    </Hub>
</Grid>

以下是我的ISupportIncrementalLoading实现:
public class IncrementalLoadingNotificationsCollection : ObservableCollection<MFSNotificationModel>, ISupportIncrementalLoading
{
    private INotificationService _notificationService;
    public IncrementalLoadingNotificationsCollection(INotificationService notificationService)
    {
        HasMoreItems = true;
        _notificationService = notificationService;
    }


    public bool HasMoreItems
    {
        get;
        private set;
    }

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

    private async Task<LoadMoreItemsResult> InnerLoadMoreItemsAsync(uint expectedCount)
    {
        var actualCount = 0;
        IList<MFSNotificationModel> 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);
            });
        }
    }
}

任何帮助解决此问题都将不胜感激。
2个回答

8
正如你在答案中提到的,StackPanel是罪魁祸首。 StackPanel控件不会约束其内容的大小。这意味着当您的ListView加载更多项时,ListView的高度也会在StackPanel内部增长,因此ListView认为每个项都是可见的,因此它会加载更多项,依此类推。
这也影响虚拟化,这是您不应该将ListView放在StackPanel内的另一个原因。根据docs
当ItemsControl的视口大小没有限制时,控件不执行虚拟化。相反,它为其集合中的每个项创建一个项容器。一些常见的不限制视口大小的容器包括Canvas、StackPanel和ScrollViewer。您可以通过直接设置ItemsControl的大小而不是让它由其父容器调整大小来在此情况下启用虚拟化。
所以你的选择是:
- 不要使用StackPanel容器。改用Grid。 - 如果您想使用StackPanel容器,则必须手动设置ListView的大小(高度)。

你真是个救命恩人。参考文档中的摘录帮了我大忙。在我的情况下,Stack Panel 和 Scroll Viewer 都引起了问题,而我一直试图修复 ListView。 :| - Sumant

2
在尝试了一番后,我成功解决了这个问题。问题出在包含 ListView 的 StackPanel 上。不知道为什么,StackPanel 的存在会导致 LoadMoreItemsAsync 被无限调用,但是一旦我将其移除,问题就得到了解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接