c# - WP 8.1 ISupportIncrementalLoading LoadMoreItemsAsync 不断被调用

标签 c# xaml windows-phone-8 infinite-scroll

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

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

下面是 View 模型的摘录

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 :

When the size of the ItemsControl's viewport isn't restricted, the control doesn't perform virtualization. Instead, it creates an item container for each item in its collection. Some common containers that don't restrict the viewport size are Canvas, StackPanel, and ScrollViewer. You can enable virtualization in this situation by setting the size of ItemsControl directly, instead of letting it be sized by its parent container.

所以你的选择是:

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

关于c# - WP 8.1 ISupportIncrementalLoading LoadMoreItemsAsync 不断被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25740079/

相关文章:

c# - 命名空间 Windows.Phone.Speech 不可用

c# - 用于序列化的 NameValueCollection 的替代方法

c# - WebHost 在 Asp Net Core 2.2 默认 API 模板的当前上下文中不存在

c# - for 循环中的每次迭代都会覆盖整个数组的值

c# - 在没有 MVVM 框架的情况下创建子窗口

c# - 类似于 jQuery UI 的模态对话框

javascript - Windows Phone 8 和 Phonegap : how to close app with back button

C# 类和实例构造函数

c# - 具有 2 个外键 Entity Framework 的表

c# - 带有 WPF 内部样式和文本 block 的矩形