我正在尝试使用以下示例实现无限滚动:
以下是我的ISupportIncrementalLoading实现:
以下是从viewmodel中提取的内容:
任何帮助解决此问题都将不胜感激。
http://www.davidbritch.com/2014/05/data-virtualisation-using.html
问题在于,在我的情况下,LoadMoreItemsAsync一直被无限调用。我是在hub上开发的(不确定这是否有影响),并使用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
};
}
}
以下是从viewmodel中提取的内容:
public IncrementalLoadingNotificationsCollection IncrementalNotifications
{
get
{
return _incrementalNotifications;
}
set
{
_incrementalNotifications = value;
if (!Equals(null) && _incrementalNotifications.Count > 0)
{
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
RaisePropertyChanged(() => IncrementalNotifications);
});
}
}
}
任何帮助解决此问题都将不胜感激。