【问题标题】:WP 8.1 ISupportIncrementalLoading LoadMoreItemsAsync keeps getting called endlesslyWP 8.1 ISupportIncrementalLoading LoadMoreItemsAsync 不断被调用
【发布时间】:2014-09-09 08:38:30
【问题描述】:

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

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

非常感谢任何解决此问题的帮助。

【问题讨论】:

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


    【解决方案1】:

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

    这也会影响虚拟化,这也是您永远不应将 ListView 放入 StackPanel 的另一个原因。根据docs

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

    所以你的选择是:

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

    【讨论】:

    • 你是个救命的人。引用文档的摘录,救了我。在我的情况下,堆栈面板和滚动查看器都导致了问题,而我一直在尝试修复列表视图。 :|
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 2020-01-07
      相关资源
      最近更新 更多