【发布时间】: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