当我尝试在列表视图中创建搜索栏时出现错误 CS1929 有人可以检查我的代码是否有错误吗?

Error CS1929 when i try to make a searchbar in a listview can someone check my code for errors?

这是我的代码,我收到错误 CS1929。我正在尝试在列表视图中创建一个搜索栏。有人可以检查我的代码是否有修复和 post 我需要使用什么代码,或者是否有另一种方法来制作搜索栏?我需要帮助!

这是我的 Xaml.cs 代码:

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));
    }
}

}

这是我的 Xmal 代码:

<?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>

LINQ 查询必须像

NameslistView.ItemsSource =
            kontakter.Where(obj => obj.Fuldenavn.Contains(keyword));

正确的做法:

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:

在 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>

否则,两个标签会重叠。

我的测试:

更新

如果要在单击取消按钮后显示所有项目,请分配事件 TextChanged

<SearchBar x:Name="MainSearchBar" SearchButtonPressed="MainSearchBar_SearchButtonPressed"  TextChanged="MainSearchBar_TextChanged"/>

后面的代码:

private void MainSearchBar_TextChanged(object sender, TextChangedEventArgs e)
{
    if (e.NewTextValue == string.Empty)
    {
        NameslistView.ItemsSource = kontakter.Where(name => (name.Fuldenavn.Contains("")));
    }
}

更新2

如果要逐字搜索,只需修改事件 TextChanged 以观察 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)));  
}