Dmitry
Dmitry

Reputation: 1165

IncrementalLoadingCollection and ViewModel with custom constructor

I have ViewModel with injection through constructor (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);
     }
}

But line Tenders = new IncrementalLoadingCollection(10); throw 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

What am I doing wrong? How to use IncrementalLoadingCollection with custom constructor?

Upvotes: 0

Views: 217

Answers (1)

Kevin Gosse
Kevin Gosse

Reputation: 39007

If you call new IncrementalLoadingCollection<MainPageViewModel, Tender>(10), then the collection will try to create a new instance of MainPageViewModel, which works only if it has a parameterless constructor (which obviously wasn't the case for you). The workaround is to provide your own instance of MainPageViewModel:

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

     var Tenders = new IncrementalLoadingCollection<MainPageViewModel, Tender>(this, 10);
 }

Upvotes: 1

Related Questions