我使用从API加载的对象实现listview。我的问题是我的列表一直加载对象,不会停止。列表不会等到用户到达其结尾。
这是我的模特:
public class AdModel
{
public string Title { get; set;
}
这是接口的实现:
public class IncrementalCollection<T> : ObservableCollection<T>, ISupportIncrementalLoading
{
private Func<uint, Task<LoadDataResult>> loadDataTask = null;
public IncrementalCollection(Func<uint, Task<LoadDataResult>> loadDataTask)
: base()
{
this.loadDataTask = loadDataTask;
this.HasMoreItems = true;
}
public bool HasMoreItems
{
get;
private set;
}
public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
return Task.Run<LoadMoreItemsResult>(async () => {
return await this.LoadMoreItemsInternalAsync(count);
}).AsAsyncOperation<LoadMoreItemsResult>();
}
private async Task<LoadMoreItemsResult> LoadMoreItemsInternalAsync(uint count)
{
var loadDataResult = await this.loadDataTask(count);
this.HasMoreItems = loadDataResult.HasMoreItems;
return new LoadMoreItemsResult { Count = loadDataResult.LoadedItemsCount };
}
}
这是viewmodel:
public class MainPageViewModel
{
private int alreadyGet = 0;
private int collectionSize = 20;
private int PageNumber = 0;
private IEnumerable<int> sourceData = null;
public MainPageViewModel(/*int collectionSize*/)
{
//this.collectionSize = collectionSize;
this.sourceData = GetData(this.collectionSize);
this.Collection = new IncrementalCollection<AdModel>(this.LoadData);
}
public IncrementalCollection<AdModel> Collection { get; private set; }
public Visibility LoaderVisibility { get; private set; }
public async Task<LoadDataResult> LoadData(uint count)
{
var items = this.sourceData.Skip(this.alreadyGet).Take((int)count).ToList();
int itemsNo = items.Count();
this.alreadyGet += itemsNo;
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
async () =>
{
this.LoaderVisibility = Visibility.Visible;
string url = "https://" + (++PageNumber).ToString();
var abc = new MainModel();
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.IfModifiedSince = DateTimeOffset.Now;
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStringAsync();
abc = JsonConvert.DeserializeObject<MainModel>(stream);
}
}
foreach (AdModel n in abc.ads)
{
this.Collection.Add(n);
}
this.LoaderVisibility = Visibility.Collapsed;
});
return new LoadDataResult
{
HasMoreItems = this.alreadyGet < this.collectionSize,
LoadedItemsCount = (uint)itemsNo
};
}
private static IEnumerable<int> GetData(int collectionSize)
{
return Enumerable.Range(1, collectionSize).OrderBy(c => c).ToList();
}
}
简单的观点:
<Grid Margin="19">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListView ItemsSource="{Binding Collection}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
Text="{Binding}" FontSize="20" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ProgressRing Visibility="{Binding LoaderVisibility}"
Margin="19" Grid.Row="1" IsActive="True"/>
</Grid>