I created a custom search bar with an entry. I need to get the text of this entry to be a search parameter.
So I created a custom property that uses the Text property of entry. But this custom property does not take the text from the custom search bar.
What am I doing wrong? Below is the code for the custom control.
Code of the search button click event of the search bar customized.
private async void SbcPesquisar_OnClicked(object sender, EventArgs e)
{
string busca = SbcPesquisar.TextSearch;
List<Promocao> Promocoes = await PromocaoService.GetListaPromocoes(busca);
LstPromocoes.ItemsSource = Promocoes;
}
Control Bihind Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MeDeiBem.Controls
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SearchBarCustom : ContentView
{
public event EventHandler ClickScope;
public SearchBarCustom ()
{
InitializeComponent ();
}
public static readonly BindableProperty TextSearchProperty =
BindableProperty.Create(
propertyName: "TextSearch",
returnType: typeof(string),
declaringType: typeof(SearchBarCustom),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: TextSearchPropertyChanged
);
public string TextSearch
{
get { return (string)GetValue(TextSearchProperty); }
set { SetValue(TextSearchProperty, value); }
}
private static void TextSearchPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var searchBarCustom = (SearchBarCustom)bindable;
searchBarCustom.textSearch.Text = (string)newValue;
}
private void On_clickScope(object sender, EventArgs e)
{
if (ClickScope != null)
{
ClickScope(sender, e);
}
}
}
}
XAML Code:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MeDeiBem.FontsAwesome;assembly=MeDeiBem"
x:Class="MeDeiBem.Controls.SearchBarCustom">
<ContentView.Content>
<Frame Margin="10, 0, 10, 0" Padding="2" BorderColor="#49c1ff" CornerRadius="5" BackgroundColor="Transparent" HeightRequest="60">
<StackLayout Orientation="Horizontal" BackgroundColor="Transparent">
<Entry x:Name="textSearch"
Placeholder="Digite o que procura"
PlaceholderColor="Black"
FontSize="Medium"
BackgroundColor="Transparent"
HorizontalOptions="FillAndExpand"
/>
<Button Text="{x:Static local:Fontes.FASearch}"
FontSize="30"
BorderWidth="0"
BackgroundColor="Transparent"
WidthRequest="60"
HorizontalOptions="End"
Clicked="On_clickScope" />
</StackLayout>
</Frame>
</ContentView.Content>
</ContentView>
XAML code where I'm using the component:
<controls:SearchBarCustom x:Name="SbcPesquisar" ClickScope="SbcPesquisar_OnClicked" />