This is my code, i get error CS1929. i am trying to make a searchbar in a listview. Can someone check my code for fixes and post what code i need to use, or if there is another way to make a searchbar? i neeeeeeed Help!
this is my Xaml.cs code:
namespace App2
{
public partial class MainPage : ContentPage
{
List<Kontakter> kontakter = new List<Kontakter>
{
new Kontakter
{
Fuldenavn = "Anja Birkelund (ANBI)",
Tlfnr = 24212504
},
new Kontakter
{
Fuldenavn = "Morten Jensen (MOJ)",
Tlfnr = 24838149
},
new Kontakter
{
Fuldenavn = "Thomas Duvall Pedersen (TPD)",
Tlfnr = 61706767
},
new Kontakter
{
Fuldenavn = "Svend-Erik Dejbjerg (SD)",
Tlfnr = 20116644
}
};
public MainPage()
{
InitializeComponent();
NameslistView.ItemsSource = kontakter;
}
private void MainSearchBar_SearchButtonPressed(object sender, EventArgs e)
{
var keyword = MainSearchBar.Text;
NameslistView.ItemsSource =
kontakter.Where(name => name.Contains(keyword));
}
}
}
this is my Xmal code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App2"
x:Class="App2.MainPage">
<StackLayout>
<SearchBar x:Name="MainSearchBar" SearchButtonPressed="MainSearchBar_SearchButtonPressed" />
<ListView x:Name="NameslistView" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Fuldenavn}" />
<Label Text="{Binding Tlfnr}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
The correct way:
private void MainSearchBar_SearchButtonPressed(object sender, EventArgs e)
{
var keyword = MainSearchBar.Text;
NameslistView.ItemsSource = kontakter.Where(obj =>(obj.Fuldenavn.Contains(keyword) || obj.Tlfnr.ToString().Contains(keyword)));
}
PS:
Modify your code in XMAL:
<StackLayout>
<SearchBar x:Name="MainSearchBar" SearchButtonPressed="MainSearchBar_SearchButtonPressed" />
<ListView x:Name="NameslistView" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Fuldenavn}" />
<Label Text="{Binding Tlfnr}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
Otherwise, the two label will overlap.
My test:
If you want to show all the item after click the cancel button , assign the Event TextChanged
<SearchBar x:Name="MainSearchBar" SearchButtonPressed="MainSearchBar_SearchButtonPressed" TextChanged="MainSearchBar_TextChanged"/>
Code behind:
private void MainSearchBar_TextChanged(object sender, TextChangedEventArgs e)
{
if (e.NewTextValue == string.Empty)
{
NameslistView.ItemsSource = kontakter.Where(name => (name.Fuldenavn.Contains("")));
}
}
if you want to search verbatim ,just modify the event TextChanged
to observe the e.NewTextValue.
private void MainSearchBar_TextChanged(object sender, TextChangedEventArgs e)
{
NameslistView.ItemsSource = kontakter.Where(obj => (obj.Fuldenavn.Contains(e.NewTextValue)|| obj.Tlfnr.ToString().Contains(e.NewTextValue)));
}