UWP C# 中用于增量加载 Collection 帮助程序的构造函数
A Constructor for Incremental Loading Collection Helpers in UWP C#
如何向 IncrementalLoadingCollection
添加构造函数。我想添加一个构造函数以将参数传递给 GetPagedItemsAsync
方法以从 API.
加载数据
我的增量加载Collection:
public class PeopleSource : IIncrementalSource<Person>
{
private readonly List<Person> people;
public PeopleSource(int maxValue)
{
// Creates an example collection.
people = new List<Person>();
for (int i = 1; i <= maxValue; i++)
{
var p = new Person { Name = "Person " + i };
people.Add(p);
}
}
public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
{
// Gets items from the collection according to pageIndex and pageSize parameters.
var result = (from p in people
select p).Skip(pageIndex * pageSize).Take(pageSize);
// Simulates a longer request...
await Task.Delay(1000);
return result;
}
}
以上代码来自微软的示例。 People
有一个构造函数,它接受一个名为 maxValue
.
的参数
var collection = new IncrementalLoadingCollection<PeopleSource, Person>();
以上代码是增量加载的初始化class。但是我在哪里传递 maxValue
参数?请帮助我...
您尝试做的事情没有多大意义,因为 functions/methods 和构造函数是根本不同的东西。
GetPagedItemsAsync
是 return 一个值(Task<IEnumerable<Person>>
)的函数。构造函数只 return 个对象的新实例;在这种情况下,PeopleSource
.
的新实例
您不能创建接受 int maxSource
的 PeopleSource
构造函数并将其 return 设为 Task<IEnumerable<Person>>
.
编辑:
作为替代方法,您可以创建一个方法来创建新的 PeopleSource
,然后执行 GetPagedItemsAsync
方法。
public static Task<IEnumerable<Person>> GetPagedItemsAsync(int maxValue, int pageIndex, int pageSize)
{
var instance = new PeopleSource(maxValue);
return instance.GetPagedItemsAsync(pageIndex, pageSize);
}
请注意,这不是最优的,因为如果您必须多次调用 GetPagedItemsAsync
,它会在您每次调用它时重新填充 PeopleSource
实例。如果数据来自数据库,这可能会非常昂贵。
通常最好创建 PeopleSource
,将其存储在一个变量中,然后根据需要多次调用 GetPagedItemsAsync
同一实例。
如何向 IncrementalLoadingCollection
添加构造函数。我想添加一个构造函数以将参数传递给 GetPagedItemsAsync
方法以从 API.
我的增量加载Collection:
public class PeopleSource : IIncrementalSource<Person>
{
private readonly List<Person> people;
public PeopleSource(int maxValue)
{
// Creates an example collection.
people = new List<Person>();
for (int i = 1; i <= maxValue; i++)
{
var p = new Person { Name = "Person " + i };
people.Add(p);
}
}
public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
{
// Gets items from the collection according to pageIndex and pageSize parameters.
var result = (from p in people
select p).Skip(pageIndex * pageSize).Take(pageSize);
// Simulates a longer request...
await Task.Delay(1000);
return result;
}
}
以上代码来自微软的示例。 People
有一个构造函数,它接受一个名为 maxValue
.
var collection = new IncrementalLoadingCollection<PeopleSource, Person>();
以上代码是增量加载的初始化class。但是我在哪里传递 maxValue
参数?请帮助我...
您尝试做的事情没有多大意义,因为 functions/methods 和构造函数是根本不同的东西。
GetPagedItemsAsync
是 return 一个值(Task<IEnumerable<Person>>
)的函数。构造函数只 return 个对象的新实例;在这种情况下,PeopleSource
.
您不能创建接受 int maxSource
的 PeopleSource
构造函数并将其 return 设为 Task<IEnumerable<Person>>
.
编辑:
作为替代方法,您可以创建一个方法来创建新的 PeopleSource
,然后执行 GetPagedItemsAsync
方法。
public static Task<IEnumerable<Person>> GetPagedItemsAsync(int maxValue, int pageIndex, int pageSize)
{
var instance = new PeopleSource(maxValue);
return instance.GetPagedItemsAsync(pageIndex, pageSize);
}
请注意,这不是最优的,因为如果您必须多次调用 GetPagedItemsAsync
,它会在您每次调用它时重新填充 PeopleSource
实例。如果数据来自数据库,这可能会非常昂贵。
通常最好创建 PeopleSource
,将其存储在一个变量中,然后根据需要多次调用 GetPagedItemsAsync
同一实例。