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

use*_*774 4 c# xaml infinite-scroll windows-phone-8

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

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

问题是在我的情况下,LoadMoreItemsAsync不断被无休止地调用.我正在集线器上开发这个(不确定这是否有所不同)并使用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>
Run Code Online (Sandbox Code Playgroud)

下面给出了我的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
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

下面给出了viewmodel的摘录

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

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

Dec*_*oon 7

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

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

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

所以你的选择是:

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