我有ViewModel注入构造函数(Prism)。
public class MainPageViewModel : ViewModelBase, IIncrementalSource<Tender>
{
public async Task<IEnumerable<Tender>> GetPagedItemsAsync(int pageIndex, int pageSize, CancellationToken cancellationToken = default(CancellationToken))
{
TendersRequest.Offset = pageIndex;
TendersRequest.Count = pageSize;
return await _dataService.GetTenders(TendersRequest);
}
public MainPageViewModel(IUnityContainer container, IDataService dataService)
{
...
var Tenders = new IncrementalLoadingCollection<MainPageViewModel, Tender>(10);
}
}
但是Tenders = new IncrementalLoadingCollection(10);抛出System.InvalidOperationException。
An exception of type 'System.InvalidOperationException' occurred in Microsoft.Toolkit.Uwp.dll but was not handled in user code
Additional information: TSource must have a parameterless constructor
我做错了什么? 如何使用IncrementalLoadingCollection和自定义构造函数?
答案 0 :(得分:1)
如果你打电话给new IncrementalLoadingCollection<MainPageViewModel, Tender>(10)
,那么这个集合会尝试创建一个MainPageViewModel
的新实例,只有当它有一个无参数构造函数时才有效(这显然不是你的情况) )。解决方法是提供您自己的MainPageViewModel
实例:
public MainPageViewModel(IUnityContainer container, IDataService dataService)
{
...
var Tenders = new IncrementalLoadingCollection<MainPageViewModel, Tender>(this, 10);
}