目前我正在开发具有GridView
的Metro应用程序,该应用程序填充滚动数据。为此,我必须实现ISupportIncrementalLoading
接口。我这样做了,我的代码工作正常。但我想知道如果发生异常,如何从Exception
函数抛出LoadMoreItemsAsync
。以下是代码段。
public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
if (count > 50 || count <= 0)
{
// default load count to be set to 50
count = 50;
}
return Task.Run<LoadMoreItemsResult>(
async () =>
{
List<MovieInfo> result = new List<MovieInfo>();
try
{
result = await ytSearcher.SearchVideos(Query, ++CurrentPage);
}
catch (Exception ex)
{
// here i want to throw that exception.
}
await this.dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
() =>
{
foreach (MovieInfo item in result)
this.Add(item);
});
return new LoadMoreItemsResult() { Count = (uint)result.Count() };
}).AsAsyncOperation<LoadMoreItemsResult>();
}
答案 0 :(得分:0)
我不是Windows运行时的经验,但我会将你的代码重构为:
public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
if (count > 50 || count <= 0)
{
// default load count to be set to 50
count = 50;
}
return LoadMoreItemsTaskAsync(count)
.AsAsyncOperation<LoadMoreItemsResult>();
}
private async Task<LoadMoreItemsResult> LoadMoreItemsTaskAsync(uint count)
{
var result = await ytSearcher.SearchVideos(Query, ++CurrentPage);
result.ForEach(i => this.Add(i));
return new LoadMoreItemsResult() { Count = (uint)result.Count };
}
请注意List<T>
具有Count
属性,其中包含列表中的项目数,而Count
方法是LINQ
扩展方法,它迭代所有要计算的项目物品数量。
当然这不能解答您的问题,但是使用这些更清晰的代码,您可能更容易理解它或获得帮助来解决它。