Hello,
If you want to make Command to be exeucted in the ViewModel, you need to add type of ICommand's property in viewModel and initialize Command in constructor.
Then, please change your parameters of SearchBar_TextChanged
like following code.
Here is a document about Commanding, you can refer ot it.
public ICommand SearchBarTextChangeCommand{ get; set; }
public MyViewModel()
{
SearchBarTextChangeCommand = new Command(SearchBar_TextChanged);
}
private void SearchBar_TextChanged(object obj)
{
var searchTerm = obj as string;
if (string.IsNullOrWhiteSpace(searchTerm))
{
searchTerm = string.Empty;
}
...
}
After that, you need to change the binding value of SearchCommand
to SearchBarTextChangeCommand
.
<SearchBar x:Name="searchBar"
HorizontalOptions="Fill"
VerticalOptions="CenterAndExpand"
Placeholder="Search ..."
Keyboard="Numeric"
SearchCommand="{Binding SearchBarTextChangeCommand}"
SearchCommandParameter="{Binding Text, Source={x:Reference searchBar}}"/>
By the way, do not forget to add bindingContext for your Viewmodel.
public NewPage1()
{
InitializeComponent();
BindingContext = new MyViewModel();
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.