jeudi 2 août 2018

Pass Generic Method With Parameter

I have the following code which allows me to pass a method as a parameter.

Items = Scroll(Items, SearchCriteria, Execute(() => ItemService.GetItemsAsync(SearchCriteria)));

My Scroll class then looks like this

public InfiniteScrollCollection<T> Scroll<T>(InfiniteScrollCollection<T> source, BaseCriteria baseCriteria, Task<List<T>> serviceCall)
{
    this.BaseSearchCriteria = baseCriteria;

    source = new InfiniteScrollCollection<T>
    {
        OnLoadMore = async () =>
        {
            IsBusy = true;

            // load the next page
            this.BaseSearchCriteria.PageIndex = source.Count / this.BaseSearchCriteria.PageSize;

            var items = await serviceCall;

            IsBusy = false;

            // return the items that need to be added
            return items;
        },
        OnCanLoadMore = () =>
        {
            return source.Count < this.MaxCalls;
        }
    };

    DownloadDataAsync(source, serviceCall);

    return source;
}

My problem arises on the line this.BaseSearchCriteria.PageIndex = source.Count / this.BaseSearchCriteria.PageSize;

I need to increase PageIndex which is a property in the parameter used in the GetItemsAsync. I can change the BaseSearchCriteria PageIndex property but the await serviceCall doesn't allow me to pass it as a parameter.

So is there a way for me to be able to change the serviceCall parameter?

I have tried changing the scroll parameters to

public InfiniteScrollCollection<T> Scroll<T>(InfiniteScrollCollection<T> source, BaseCriteria baseCriteria, Func<BaseCriteria, Task<List<T>>> serviceCall)

which then allows me to pass the updated criteria to serviceCall

var items = await serviceCall(this.BaseSearchCriteria);

But then I don't know how to pass the method/func into the initial code shown in the first code snippet.

EDIT: I've change the initial code to this and it seems to be working.

Items = Scroll(Items, SearchCriteria, 
            new Func<BaseCriteria, Task<List<Data.Entities.Item>>>(x => ItemService.GetItemsAsync(SearchCriteria)));





Aucun commentaire:

Enregistrer un commentaire