首页 > 解决方案 > Xamarin Forms C# ListView 搜索

问题描述

Xamarin Forms C# ListView 搜索
大家好,当我按名称进行过滤时,我想从 ListView 进行搜索我希望建议出现在 listview 中我尝试这样做但对我不起作用!我不明白是什么问题..

问题截图:https ://i.imgur.com/vhpt8Cp.jpg

Xamarin Forms C# ListView Search   

大家好,当我按名称进行过滤时,我想从 ListView 进行搜索我希望建议出现在列表视图中我试图这样做但对我不起作用!我不明白是什么问题..

    Model> echeance.cs:
    ------------

    namespace Appz.Model
    {
        public class Echeance
        {

            [PrimaryKey, AutoIncrement]
            public int ChkId { get; set; }

            public string ChkChoise { get; set; }

            [MaxLength(50)]
            public string ChkNumber { get; set; }

            public string ChkAmount { get; set; }

            [MaxLength(50)]
            public string BeneficiaryName { get; set; }

            public string AgencyType { get; set; }

            public DateTime ChkDueDate { get; set; }

            public DateTime ChkReleaseDate { get; set; }

            public string ChkImage { get; set; }


        }
    }





    EcheanceView.Xaml:
    -----------------

    <?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:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" 
        ios:Page.UseSafeArea="true"
                 x:Class="Appz.View.EcheanceView">
        <ContentPage.Content>
            <StackLayout Padding="10,0,10,0">
                <SearchBar TextChanged="Handle_TextChanged"></SearchBar>
                <ListView  ItemsSource="{Binding EcheancesList}" IsPullToRefreshEnabled="True"  x:Name="ListEcheances" HasUnevenRows="true" SeparatorVisibility="Default" ItemTapped="OnItemSelected">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Orientation="Horizontal">
                                    <Image Source="{Binding ChkImage}" HeightRequest="50" WidthRequest="50"  />
                                    <StackLayout HorizontalOptions="StartAndExpand">
                                        <Label Text="{Binding BeneficiaryName}"
                                           FontAttributes="Bold" />
                                        <Label Text="{Binding AgencyType}"
                                           TextColor="Gray" />
                                    </StackLayout>
                                    <Button Text="Follow Me"
                                        BorderColor="Silver"
                                       FontSize="Small"
                                       TextColor="White"
                                       BackgroundColor="#3399ff"
                                       VerticalOptions="Center"
                                      />
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </StackLayout>

        </ContentPage.Content>
    </ContentPage>





    EcheanceView.cs:
    ---------------

    void Handle_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
            {


                using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
                {               
                    var echeances = conn.Table<Echeance>().ToList();
                    //ListEcheances.ItemsSource = echeances;

                    if (string.IsNullOrWhiteSpace(e.NewTextValue))
                        ListEcheances.ItemsSource = echeances;
                    else
                        ListEcheances.ItemsSource = echeances.Where(i => i.BeneficiaryName.Contains(e.NewTextValue));

                    ListEcheances.EndRefresh();

                }                    

            }

有人帮助我请提前谢谢!

标签: c#androidxamarinxamarin.formsxamarin.android

解决方案


在您的情况下,似乎某些项目的 BeneficiaryName 为空。


推荐阅读