0

Unlike the behavior on windows platform my search handler does not trigger my command method on android platform

I tried putting gesture recognizers, but this does not work too.

Method "SelectGymCommand" on my viewmodel class adds the selected item to the SelectedGyms list which is binded to listview. This works perfectly on windows plataform, but in android plataform the listview is not updated.

> `<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:utils="clr-namespace:BehaviorMauiApp.Utils"
xmlns:viewModel="clr-namespace:BehaviorMauiApp.ViewModel"
xmlns:model="clr-namespace:BehaviorMauiApp.Model"
x:Class="BehaviorMauiApp.View.FitnessPage" 
x:DataType="viewModel:GymViewModel"
Title="FitnessPage">
>     <Shell.SearchHandler>
>         <utils:SearchGymHandler
x:Name="searchGymHandler"
GymToSearch="{Binding ListGym}"
Placeholder="pesquisar"
Command="{Binding SelectGymCommand}"
CommandParameter="{Binding Source={x:Reference searchGymHandler}, Path=SelectedItem}">
>             <utils:SearchGymHandler.ItemTemplate>
>                 <DataTemplate   x:DataType="model:Gym" >
>                     <Grid Padding="8" RowDefinitions="*">

>                         <Label Margin="8" TextColor="Gray" Text="{Binding Description}" >
>                         </Label>
>                     </Grid>
>                 </DataTemplate>
>             </utils:SearchGymHandler.ItemTemplate>
>         </utils:SearchGymHandler>
>     </Shell.SearchHandler>
>     <VerticalStackLayout>
>         <Image Aspect="Center" Margin="0,32,0,0" Source="outline_fitness_center_white_48dp.png" />
>         <ListView ItemsSource="{Binding SelectedGyms}">
>             <ListView.ItemTemplate>
>                 <DataTemplate x:DataType="model:Gym">
>                     <ViewCell>
>                         <Label Margin="8" TextColor="Gray" Text="{Binding Description}"></Label>
>                     </ViewCell>
>                 </DataTemplate>
>             </ListView.ItemTemplate>
>         </ListView>
>     </VerticalStackLayout>
> </ContentPage>`

SearchGymHandler

namespace BehaviorMauiApp.Utils;

    public class SearchGymHandler : SearchHandler
    {
        public static BindableProperty GymToSearchProperty = BindableProperty.Create(
            nameof(GymToSearch), typeof(List<Gym>), typeof(SearchGymHandler),new List<Gym>());
        public List<Gym> GymToSearch
        {
            get => (List<Gym>)GetValue(GymToSearchProperty) ;
            set => SetValue(GymToSearchProperty, value);
        }
    
        protected override void OnQueryChanged(string oldValue, string newValue)
        {
            base.OnQueryChanged(oldValue, newValue);
    
            ItemsSource = GymToSearch.Where(a => a.Description.Contains(newValue, StringComparison.OrdinalIgnoreCase)).ToList();
        }
    }

ViewModel

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace BehaviorMauiApp.ViewModel;

public class GymViewModel : ObservableObject
{
    List<Gym> listGym;
    public List<Gym> ListGym
    {
        get => listGym; set
        {
            listGym = value;
            OnPropertyChanged();
        }
    }
    public ObservableCollection<Gym> SelectedGyms { get; }
    public ICommand SelectGymCommand { get; }

    public GymViewModel(RestService restService)
    {
        ListGym = restService.GetGymList();
        SelectGymCommand = new AsyncRelayCommand<Gym>(Select);
        SelectedGyms = new();
    }

    private Task Select(Gym selectedGym)
    {
        SelectedGyms.Add(selectedGym);
        return Task.CompletedTask;
    }
}
m martins
  • 1
  • 1

1 Answers1

0

Based on your code, I created a demo to achieve this function.

You can refer to the following code:

SearchGymHandler.cs

public class SearchGymHandler : SearchHandler 
    {
        public IList<Gym> GymToSearch { get; set; }

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

            if (string.IsNullOrWhiteSpace(newValue))
            {
                ItemsSource = null;
            }
            else
            {
                ItemsSource = GymToSearch
                    .Where(animal => animal.Description.ToLower().Contains(newValue.ToLower()))
                    .ToList<Gym>();
            }
        }
    }

SourceData.cs

public static class SourceData 
    {
        public static IList<Gym> Items { get; private set; }

        static SourceData()
        {
            Items = new List<Gym>();
            Items.Add(new Gym { Description = "test1" });
            Items.Add(new Gym { Description = "test2" });
            Items.Add(new Gym { Description = "test3" });
            Items.Add(new Gym { Description = "test4" });
            Items.Add(new Gym { Description = "test5" });
            Items.Add(new Gym { Description = "test6" });
        }
    }

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewmodels="clr-namespace:MVVMMauiApp.viewmodels"
             xmlns:local="clr-namespace:MVVMMauiApp"
             xmlns:data="clr-namespace:MVVMMauiApp.Data"
             x:Class="MVVMMauiApp.MainPage">

        <Shell.SearchHandler>
            <local:SearchGymHandler  SearchBoxVisibility="Expanded"
                ShowsResults="True"
                x:Name="searchGymHandler"
                GymToSearch="{x:Static data:SourceData.Items}"
                Placeholder="pesquisar"
                ItemTemplate="{StaticResource SearchTemplate}"
                >

            </local:SearchGymHandler>
        </Shell.SearchHandler>

        <ListView   ItemsSource="{x:Static data:SourceData.Items}">
            <ListView.ItemTemplate>
                    <DataTemplate >
                        <ViewCell>
                            <Label Margin="8" TextColor="Gray" Text="{Binding Description}"></Label>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
        </ListView>

</ContentPage>

App.xaml

<?xml version = "1.0" encoding = "UTF-8" ?> 
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MVVMMauiApp"
             x:Class="MVVMMauiApp.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
                <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>


            <DataTemplate x:Key="SearchTemplate">
                <Grid Padding="8" RowDefinitions="*">
                    <Label Margin="8" TextColor="Gray" Text="{Binding Description}" > </Label>
                </Grid>
            </DataTemplate>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Note:

For more information, you can check document:.NET MAUI Shell search.

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19