0

我有通过构造函数(Prism)注入的ViewModel。

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 与自定义构造函数一起使用?

4

1 回答 1

1

如果您调用new IncrementalLoadingCollection<MainPageViewModel, Tender>(10),则该集合将尝试创建 的新实例MainPageViewModel,该实例仅在它具有无参数构造函数时才有效(显然您不是这种情况)。解决方法是提供您自己的实例MainPageViewModel

 public MainPageViewModel(IUnityContainer container, IDataService dataService)
 {
     ... 

     var Tenders = new IncrementalLoadingCollection<MainPageViewModel, Tender>(this, 10);
 }
于 2016-12-04T14:23:15.470 回答