I am trying to implement a Xamarin.Forms search bar with realtime results, but have found very little success! Can you please help me understand how to make one of these approaches work? All scenarios are based on "Xamarin.Forms - SearchBar Demos" from MS Docs (I cannot post links, yet).
Note about my ViewModel:
I have been using this INotifyPropertyChanged implementation, which I do not believe would interfere with intended search bar operation:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}
Adding the MS Docs sample version's has not seemed to change anything.
1) Based on sample "SearchBarXamlMvvmPage"
Does not search in realtime,
Does not allow search to clear or reset,
"Search Button" and "Clear Search Button" do not seem to trigger any command or event
CollectionView loses highlighted items after SearchCommand
My Xaml:
<SearchBar x:Name="searchBar"
Placeholder="Search..."
SearchCommand="{Binding SearchCommand}"
SearchCommandParameter="{Binding Text, Source={x:Reference searchBar}}"/>
<CollectionView
x:Name="MyListView"
ItemsSource="{Binding MyList}"
SelectionMode="Multiple"
SelectedItem="{Binding MySelectedItem, Mode=TwoWay}"
SelectedItems="{Binding MySelectedItems, Mode=TwoWay}"
SelectionChangedCommand="{Binding MySelectionChangedCommand}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid...etc...
My View Model:
public ICommand SearchCommand { get; }
//in constructor...
SearchCommand = new Command<string>(OnSearchCommand);
//related method
public void OnSearchCommand(string query)
{
MyList = new ObservableCollection<MyClass>(App.MyDataService.GetByQueryAsync(query).Result);
}
2) Based on "SearchBarXamlPage"
Fails in all the same ways as effort #1
My Xaml:
<SearchBar x:Name="searchBar"
SearchButtonPressed="OnSearchButtonPressed"/>
<CollectionView etc...
My Xaml.cs:
void OnSearchButtonPressed(object sender, EventArgs e)
{
ViewModelLocator.MyListVM.OnSearchCommand(searchBar.Text);
//(ViewModelLocator = static class contains one of each view model)
}
This View Model is the same as above.
3) Attempting an Event-to-Command Behavior
Does not trigger command in View Model, so there is no function
My Behavior:
public class EventToCommandBehavior : BindableBehavior<View>
{
public static BindableProperty EventNameProperty =
BindableProperty.CreateAttached("EventName", typeof(string), typeof(EventToCommandBehavior), null,
BindingMode.OneWay);
public static BindableProperty CommandProperty =
BindableProperty.CreateAttached("Command", typeof(ICommand), typeof(EventToCommandBehavior), null,
BindingMode.OneWay);
public static BindableProperty CommandParameterProperty =
BindableProperty.CreateAttached("CommandParameter", typeof(object), typeof(EventToCommandBehavior), null,
BindingMode.OneWay);
etc...
My Xaml:
<SearchBar x:Name="searchBar">
<SearchBar.Behaviors>
<behaviors:EventToCommandBehavior
EventName="TextChanged"
Command="{Binding SearchCommand}"
CommandParameter="{Binding Source={x:Reference searchBar}, Path=Text}">
</behaviors:EventToCommandBehavior>
</SearchBar.Behaviors>
</SearchBar>
<CollectionView etc...
This View Model is the same as above.
I had a home-brewed search entry working real time, but it also forgets which items are selected if I navigate away, so I'm back to fighting with the built-in product. Any help would be appreciated! Thanks!