How to use a searchHandler class with parametered constructor

iooovoltsiooovolts Member ✭✭✭

Can someone please please help me overcome this hurdle or potentially suggest a better way? I am trying to implement search functionality in an app, and I am having problems using my repository code for CRUD. Please see below code:
The problem is that because of this parameter I cannot use that search handler in my XAML like the Xaminals demo app shows (as I will also show below). I just want to be able to get the ItemSelected and then use the same Singleton instance of UnitOfWork created by my IoC container to do the DB operations. I tried accessing ItemSelected from the xaml so I could just bind it to a Command but it seems you cannot do that :neutral:

Search handler class

public class IngredientsSearchHandler : SearchHandler
    {
        private readonly IUnitOfWork _unitOfWork;

        public IngredientsSearchHandler(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        protected override void OnQueryChanged(string oldValue, string newValue)
        {
            base.OnQueryChanged(oldValue, newValue);

            if (string.IsNullOrWhiteSpace(newValue))
            {
                ItemsSource = null;
            }
            else
            {
                ItemsSource = _unitOfWork.IngredientRepository.GetAll().Where(x => x.Name.ToLower().Contains(newValue.ToLower())).ToList();
            }
        }

        protected override async void OnItemSelected(object item)
        {
            base.OnItemSelected(item);
            await _unitOfWork.IngredientRepository.AddAsync(item as Ingredient);
        }
    }

IngredientsView xaml

<Shell.SearchHandler>
        <controls:IngredientsSearchHandler Placeholder="Enter ingredient.."
                                           ShowsResults="true"
                                           DisplayMemberName="Name"
                                           Keyboard="Text">
            <controls:IngredientsSearchHandler.ItemTemplate>
                <DataTemplate>
                    <Grid Padding="10">
                        <Label Text="{Binding Name}"
                               FontAttributes="Bold"/>
                    </Grid>
                </DataTemplate>
            </controls:IngredientsSearchHandler.ItemTemplate>
        </controls:IngredientsSearchHandler>
    </Shell.SearchHandler>

Type 'IngredientsSearchHandler' is not usable as an object element because it is not public or does not define a public parameterless constructor or a type converter

Answers

  • LeijaeLeijae Member ✭✭

    seconded on this question, but I'd like to implement Mediatr to do the query and commands

Sign In or Register to comment.