Skip to content

Instantly share code, notes, and snippets.

@AlejandroRuiz
Created July 29, 2019 06:04
Show Gist options
  • Save AlejandroRuiz/7483c77fd78e6bcf8982ab6032c69263 to your computer and use it in GitHub Desktop.
Save AlejandroRuiz/7483c77fd78e6bcf8982ab6032c69263 to your computer and use it in GitHub Desktop.
Xamarin Forms Custom SearchHandler
public class GPlayMusicSearchHandler : SearchHandler
{
public GPlayMusicSearchHandler()
{
}
protected override void OnQueryChanged(string oldValue, string newValue)
{
base.OnQueryChanged(oldValue, newValue);
if (string.IsNullOrWhiteSpace(newValue))
{
ItemsSource = Artists.OrderBy(item => item.Name);
}
else
{
ItemsSource = Artists.Where(item => item.Name.ToLower().Contains(newValue.ToLower())).OrderBy(item => item.Name).ToList<Artist>();
}
}
protected override void OnItemSelected(object item)
{
base.OnItemSelected(item);
//YOUR LOGIC
}
}
<ContentPage>
<Shell.SearchHandler>
<services:GPlayMusicSearchHandler Placeholder="Search music"
ShowsResults="true"
SearchBoxVisibility="Collapsible"
CancelButtonColor="Black"
TextColor="Black"
DisplayMemberName="Name">
<services:GPlayMusicSearchHandler.ItemTemplate>
<DataTemplate>
<views:ArtistSearchItemView />
</DataTemplate>
</services:GPlayMusicSearchHandler.ItemTemplate>
</services:GPlayMusicSearchHandler>
</Shell.SearchHandler>
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment