How to add search functionality to dotnet maui application?

Rivindhu Kudamage 1 Reputation point
2022-08-24T14:30:44.273+00:00

I'm creating a small notepad app by using dotnet MAUI. I already created CRUD functionality to the app. Now I want to add a search functionality to search notes.

This is my code that I used to get notes.
[ICommand]
private async void GetNoteList()
{
var noteList = await _noteService.GetNoteList();
if (noteList?.Count > 0)
{
Notes.Clear();
foreach (var note in noteList)
{
Notes.Add(note);
}
}
}

And I try to create the search functionality
[ICommand]
public async void NoteSearchItems()
{
var noteList = await _noteService.GetNoteList();
var searchedName = noteList.Where(value => value.Description.ToLowerInvariant().Contains("searchForNotes")).ToList();

        //Clearing previous notes  
        Notes.Clear();  
        foreach (var note in searchedName)  
        {  
            //add Notes searched to List  
            Notes.Add(note);  
        }  
    }  

And this is my UI
<SearchBar Grid.Row="0"
Grid.Column="0"
Margin="0,0,0,10"
x:Name="searchForNotes"
SearchCommand="{Binding NoteSearchItemsCommand}"
Placeholder="Search notes..." />
<CollectionView Grid.Row="1"
Grid.Column="0"
HorizontalOptions="CenterAndExpand"
ItemsSource="{Binding Notes}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="models:NoteModel">
<Border Margin="0,0,15,5"
Stroke="#696969"
StrokeThickness="2"
Background="#f5f5f5"
HeightRequest="200"
WidthRequest="250"
Padding="10"

                    <Border.StrokeShape>  
                        <RoundRectangle CornerRadius="2,2,2,2" />  
                    </Border.StrokeShape>  
                    <Border.GestureRecognizers>  
                        <TapGestureRecognizer Command="{Binding Source={x:RelativeSource   
                            AncestorType={x:Type viewmodels:NoteListPageViewModel}},   
                            Path=DisplayActionCommand}" CommandParameter="{Binding .}" />  
                    </Border.GestureRecognizers>  
                    <Label Text="{Binding Description}"  
                           TextColor="Black"  
                           FontSize="14"  
                           FontAttributes="None"  
                           />  
                </Border>  
            </DataTemplate>  
        </CollectionView.ItemTemplate>  
    </CollectionView>  

But the search functionality is not working correctly. I need your help.
Thank you!

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
{count} votes