WP 8.1 ISupportIncrementalLoading LoadMoreItemsAsync不断获取调用不休 [英] WP 8.1 ISupportIncrementalLoading LoadMoreItemsAsync keeps getting called endlessly

查看:331
本文介绍了WP 8.1 ISupportIncrementalLoading LoadMoreItemsAsync不断获取调用不休的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的例子来实现无限滚动



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



问题是,在我的情况的 LoadMoreItemsAsync不断得到所谓不休。我的一个枢纽(不知道这是否有差别)制定本使用MVVMLight。下面给出的是我的代码



的.xaml



<预类=郎XAML prettyprint-覆盖> <第
X:类=MyFileServer.UniversalApp.AppHubPage
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns :X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:本地=使用:MyFileServer.UniversalApp
的xmlns:D =HTTP://模式。 microsoft.com/expression/blend/2008
的xmlns:MC =http://schemas.openxmlformats.org/markup-compatibility/2006
MC:可忽略=D
背景={ThemeResource ApplicationPageBackgroundThemeBrush}
的DataContext ={绑定源= {StaticResource的MFSViewModelLocator},路径= AppHub}>

<网格和GT;
<集线器头=我的文件服务器>
< HubSection X:NAME =MFSNotifications标题=通知>
<&DataTemplate的GT;
<&StackPanel的GT;
< ListView的X:名称=通知的ItemsSource ={结合IncrementalNotifications}>
< ListView.ItemTemplate>
<&DataTemplate的GT;
< TextBlock的文本={结合NotificationDescription}/>
< / DataTemplate中>
< /ListView.ItemTemplate>
< /&的ListView GT;
< / StackPanel的>
< / DataTemplate中>
< / HubSection>
< HubSection X:NAME =MFSFiles头=文件>< / HubSection>
< /集线器>
< /网格和GT;





下面给出的是我实现的ISupportIncrementalLoading



<预类=郎-C#prettyprint-覆盖> 公共类IncrementalLoadingNotificationsCollection:的ObservableCollection< MFSNotificationModel>中ISupportIncrementalLoading
{
私人INotificationService _notificationService;
公共IncrementalLoadingNotificationsCollection(INotificationService notificationService)
{
HasMoreItems = TRUE;
_notificationService = notificationService;
}


公共BOOL HasMoreItems
{
搞定;
私人集;
}

公共IAsyncOperation< LoadMoreItemsResult> LoadMoreItemsAsync(UINT计数)
{
返回InnerLoadMoreItemsAsync(计数).AsAsyncOperation();
}

私人异步任务< LoadMoreItemsResult> InnerLoadMoreItemsAsync(UINT expectedCount)
{
变种actualCount = 0;
&IList的LT; MFSNotificationModel>通知;


{
=通知等待_notificationService.GetNotificationsAsync(ConfigurationSettings.AccessToken,8);
}
赶上(例外)
{
HasMoreItems = FALSE;
抛出;
}

如果(通知= NULL&放大器;!&安培; notifications.Any())
{
的foreach(VAR的通知的通知)
{
添加(通知);
}

actualCount + = notifications.Count;
// _ photoStartIndex + =(UINT)actualCount;
}
,否则
{
HasMoreItems = FALSE;
}

返回新LoadMoreItemsResult
{
计数=(UINT)actualCount
};
}
}



下面给出的是从视图模型提取物



<预类=郎-C#prettyprint-覆盖> 公共IncrementalLoadingNotificationsCollection IncrementalNotifications
{
得到
{
返回_incrementalNotifications;
}

{
_incrementalNotifications =价值;
如果(等于(空)及!&安培; _incrementalNotifications.Count大于0)
{
DispatcherHelper.CheckBeginInvokeOnUI(()=>
{
RaisePropertyChanged (()=> IncrementalNotifications);
});
}
}
}



任何帮助解决这大加赞赏。


解决方案

玩弄我设法解决这个问题之后。问题是StackPanel中,其中的ListView是。我不知道为什么,但由于某种原因,StackPanel中的存在引起了LoadMoreItemsAsync获取调用不休,一旦我删除它,它工作得很好。


I am trying to implement infinite scrolling using the following example

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

The problem is that in my case LoadMoreItemsAsync keeps getting called endlessly. I am developing this on a hub (not sure if this makes a difference) and using MVVMLight. Given below is my code

.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>

Given below is my implementation of 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
        };
    }
}

Given below is the extract from the viewmodel

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

Any help to solve this is much appreciated.

解决方案

After playing around I managed to solve this. The problem was the StackPanel in which the ListView was in. I dont know why but for some reason the existence of the StackPanel caused the LoadMoreItemsAsync getting called endlessly and as soon as I removed it, it worked fine.

这篇关于WP 8.1 ISupportIncrementalLoading LoadMoreItemsAsync不断获取调用不休的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆