'Binding XML to ModelViewControl

I want the method of a command to be in the ViewModel class, but the class is not finding it and it is showing this error. what can i do to make it work

Github: https://github.com/IagoAntunes/FashionShop/tree/master/LojaRoupas/LojaRoupas

error: No property ,BindableProperty or event found for "SearchButtonPressed", or mismatching type between value and property

SHOP.XML

SearchButtonPressed="{Binding PesquisarButton}" TextChanged="{Binding Pesquisar}"
<SearchBar Grid.Column="0" Grid.Row="1" Margin="-5,10,0,10" BackgroundColor="Transparent" SearchButtonPressed="{Binding PesquisarButton}" TextChanged="{Binding Pesquisar}"/>

SHOP.cs

BindingContext = new ViewModel.ShopViewModel();

SHOPVIEWMODEL.cs

Here I have the method that will execute that command


public class ShopViewModel
{
        private void PesquisarButton(object sender,EventArgs args)
        {
            var Resultado = ListaRoupas.Where((b)=>b.Nome.Contains(((SearchBar)sender).Text)).ToList<Roupa>();
            Preencher(Resultado);
        }
        private void Pesquisar(object sender, TextChangedEventArgs args)
        {
            var Resultado = ListaRoupas.Where((b) => b.Nome.Contains(((SearchBar)sender).Text)).ToList<Roupa>();
            Preencher(Resultado);
        }
}

obs:I'm just showing the essential parts of the code



Solution 1:[1]

you need to bind text with the ViewModel property and then when the user clicks the search button you can get a text from that property.

please refer this official doc

Xamarin.Forms SearchBar

Solution 2:[2]

SearchButtonPressed="{Binding PesquisarButton}" TextChanged="{Binding Pesquisar}"

Change to

SearchCommand="{Binding PesquisarButton}" Text="{Binding Pesquisar, Mode=TwoWay}"
public class ShopViewModel :INotifyPropertyChanged
{
       public Command PesquisarButton => new Command(() =>
       {
            var Resultado = ListaRoupas.Where((b)=>b.Nome.Contains(Pesquisar).ToList<Roupa>();
            Preencher(Resultado);
        }

       public string Pesquisar{get;set;}

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs((propertyName)));
        }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KD_
Solution 2