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

2023-12-21

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

http://www.davidbritch.com/2014/05/data-virtualization-using.html 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>

下面给出的是我的 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
        };
    }
}

下面给出的是视图模型的摘录

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 中的另一个原因。根据docs http://msdn.microsoft.com/en-gb/library/windows/apps/hh780657.aspx:

当 ItemsControl 视口的大小不受限制时,该控件不执行虚拟化。相反,它为其集合中的每个项目创建一个项目容器。一些不限制视口大小的常见容器包括 Canvas、StackPanel 和 ScrollViewer。在这种情况下,您可以通过直接设置 ItemsControl 的大小来启用虚拟化,而不是让它由其父容器调整大小。

所以你的选择是:

  • 不要使用 StackPanel 容器。请改用网格。
  • 如果要使用 StackPanel 容器,则必须手动设置 ListView 的大小(高度)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WP 8.1 ISupportIncrementalLoading LoadMoreItemsAsync 不断被无休止地调用 的相关文章

随机推荐