Hello,
I want to refresh my listview. A list is loading by api call. And that is fine.
But i want to apply search functionality, the search parameters are available in xaml.cs page and api calling is taking place in viewmodel.
How can i trigger api call in viewmodel with those xaml.cs parameter?
(How to force to call api in viewmodel with the parameter of xaml.cs page)
Suggest please.
Regards,
Gourav
<ContentPage>
<Grid x:Name="MainGrid" Margin="0" RowSpacing="0">
<SearchBar Placeholder="Search here to filter" >
<SearchBar.Behaviors>
<local:EventToCommandBehavior EventName="TextChanged"
Command="{Binding FilterCommand}"
CommandParameter="{x:Reference MainGrid}" />
</SearchBar.Behaviors>
</SearchBar>
<sync:SfListView x:Name="listView" ItemsSource="{Binding Items}"/>
</Grid>
</ContentPage> |
public class BookInfoViewModel : INotifyPropertyChanged
{
private Command<object> filterCommand;
private string searchText;
#region Properties
public Command<object> FilterCommand
{
get { return filterCommand; }
protected set { filterCommand = value; }
}
#endregion
#region Constructor
public BookInfoViewModel()
{
filterCommand = new Command<object>(OnTextChanged);
}
#endregion
private void OnTextChanged(object obj)
{
var grid = (obj as Grid);
var searchBar = grid.Children[0] as SearchBar;
searchText = searchBar.Text;
var listview = grid.Children[1] as SfListView;
if (listview.DataSource != null)
{
listview.DataSource.Filter = FilterBooks;
listview.DataSource.RefreshFilter();
}
}
private bool FilterBooks(object obj)
{
if (searchText == null)
return true;
var taskInfo = obj as BookInfo;
return (taskInfo.BookName.ToLower().Contains(searchText.ToLower())
|| taskInfo.BookDescription.ToLower().Contains(searchText.ToLower()));
}
} |
<SearchBar x:Name="filterText"
IsVisible="true"
TextChanged="OnfilterTextChanged"
Placeholder="Search here to filter"/> |
private void OnfilterTextChanged(object sender, TextChangedEventArgs e)
{
var grid = (sender as SearchBar).Parent as Grid;
(this.BindingContext as BookInfoViewModel).FilterCommand.Execute(grid);
} |