WP 8.1 I支持增量加载加载更多项目同步不断被调用

本文关键字:加载 同步 项目 调用 支持 WP | 更新日期: 2023-09-27 18:21:07

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

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

问题是,在我的案例中,LoadMoreItemssync不断被调用。我正在一个集线器上开发这个(不确定这是否有什么不同),并使用MVVMLight。下面给出的是我的代码

.xml

<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);
            });
        }
    }
}

我们非常感谢为解决这一问题提供的任何帮助。

WP 8.1 I支持增量加载加载更多项目同步不断被调用

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

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

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

所以你的选择是:

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

玩了一番之后,我设法解决了这个问题。问题是ListView所在的StackPanel。我不知道为什么,但由于某种原因,StackPanel的存在导致LoadMoreItemsSync被无休止地调用,我一删除它,它就工作得很好。