简体   繁体   English

搜索在 ios xamarin.forms 中不起作用

[英]search is not working in ios xamarin.forms

i have a searchbar in my app.我的应用程序中有一个搜索栏。 the code for searching is as follows:搜索代码如下:

 private void search_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (list_total_reg_std.Count != 0)
            {
                if (string.IsNullOrEmpty(e.NewTextValue))
                {
                    if (filtered == false)
                        registeredstdslist.ItemsSource = list_total_reg_std;
                    else
                        registeredstdslist.ItemsSource = list_filtered_reg_std;
                }

                else
                {
                    if (filtered == false)
                        registeredstdslist.ItemsSource = list_total_reg_std.Where(x => x.name.ToLower().Contains(e.NewTextValue) || x.stdid.ToLower().Contains(e.NewTextValue) || x.edlevel.ToString().ToLower().Contains(e.NewTextValue));
                    else
                        registeredstdslist.ItemsSource = list_filtered_reg_std.Where(x => x.name.ToLower().Contains(e.NewTextValue) || x.stdid.ToLower().Contains(e.NewTextValue) || x.edlevel.ToString().ToLower().Contains(e.NewTextValue));

                }
            }
}

this is my xaml:这是我的 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"
             x:Class="ALNahrainAlphaApp.Accounting"
             xmlns:images="clr-namespace:ALNahrainAlphaApp;assembly=ALNahrainAlphaApp">
    <ContentPage.ToolbarItems >

        <ToolbarItem Order="Secondary"
                     Text="logout"
                     Priority="2"
                Clicked="ToolbarItem_Clicked"
                    />
    </ContentPage.ToolbarItems>
    <ContentPage.Resources>
        <Style TargetType="Grid">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal" />
                        <VisualState x:Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor"
                                        Value="#f4f0ec" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </ContentPage.Resources>
    <ContentPage.Content>

        <AbsoluteLayout Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <StackLayout BackgroundColor="White" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
                
                <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" >
                    <SearchBar HorizontalOptions="FillAndExpand" x:Name="search"  TextChanged="search_TextChanged"/>
                    <RefreshView x:Name="refresh" >
                        <CollectionView x:Name="registeredstdslist" SelectionChanged="registeredstdslist_SelectionChanged" SelectionMode="Single" >
                            <CollectionView.Header>
                                <Grid Padding="2" ColumnSpacing="1" RowSpacing="1">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="35"/>

                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="0.4*"/>
                                        <ColumnDefinition Width="0.5*"/>
                                        <ColumnDefinition Width="0.5*"/>
                                        <ColumnDefinition Width="0.3*"/>
                                        <ColumnDefinition Width="0.2*"/>
                                        <ColumnDefinition Width="0.3*"/>
                                    </Grid.ColumnDefinitions>
                                    <Label 
                                Grid.Column="0"
                                Text="StdID"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                               />
                                    <Label 
                                Grid.Column="1" 
                                Text="Name"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                />
                                    <Label 
                                Grid.Column="2"
                                Text="Reg Date"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                />
                                    <Label
                                Grid.Column="3"
                                Text="Edlevel"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                />
                                    <Label
                                Grid.Column="4"
                                Text="Grade"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                />
                                    <Label
                                Grid.Column="5"
                                Text="Status"
                                 TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                />

                                </Grid>

                            </CollectionView.Header>
                            <CollectionView.ItemTemplate>
                                <DataTemplate >
                                    <Grid ColumnSpacing="1" RowSpacing="0" Padding="9,0,0,0" >
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="40"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="0.4*"/>
                                            <ColumnDefinition Width="0.5*"/>
                                            <ColumnDefinition Width="0.5*"/>
                                            <ColumnDefinition Width="0.3*"/>
                                            <ColumnDefinition Width="0.2*"/>
                                            <ColumnDefinition Width="0.3*"/>

                                        </Grid.ColumnDefinitions>
                                        <Label 
                                    Grid.Column="0"
                                    Text="{Binding stdid}"
                                    TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                        FontSize="13"
                                    />
                                        <Label 
                                    Grid.Column="1"
                                    Text="{Binding name}"
                                    TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                        FontSize="13"
                                    />
                                        <Label 
                                    Grid.Column="2"
                                    Text="{Binding DateofReg}"
                                    TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                        FontSize="13"
                                       
                                    />
                                        <Label 
                                    Grid.Column="3"
                                    Text="{Binding edlevel}"
                                    TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                        FontSize="13"
                                    />
                                        <Label 
                                    Grid.Column="4"
                                    Text="{Binding grade}"
                                    TextColor="Black"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    LineBreakMode="TailTruncation"
                                        Padding="10"
                                        FontSize="13"
                                    />
                                        <Image
                                    
                                    Grid.Column="5"
                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    HeightRequest="20"
                                    WidthRequest="20"
                                   >
                                            <Image.Triggers>
                                                <DataTrigger TargetType="Image" Binding="{Binding status}" Value="false" >
                                                    <Setter Property="Source" Value="{OnPlatform Android=redtik.png,iOS=redtik.png}"/>
                                                </DataTrigger>
                                                <DataTrigger TargetType="Image" Binding="{Binding status}" Value="true" >
                                                    <Setter Property="Source" Value="{OnPlatform Android=greentik.png, iOS=greentik.png}"/>

                                                </DataTrigger>
                                            </Image.Triggers>
                                        </Image>

                                    </Grid>
                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>
                    </RefreshView>
                    <StackLayout VerticalOptions="EndAndExpand"  HorizontalOptions="FillAndExpand" Orientation="Horizontal" BackgroundColor="#0d98ba">
                        <StackLayout Style="{StaticResource ButtonNavigationBarStackLayoutStyle}" x:Name="stckAddStd">
                            <Image Margin="0,10,0,5" x:Name="imgAdd" Style="{StaticResource ButtonNavigationBarImageStyle}" />
                            
                        </StackLayout>
                        <StackLayout Style="{StaticResource ButtonNavigationBarStackLayoutStyle}" x:Name="stckfilter">
                            <Image Margin="0,10,0,10" x:Name="imgfilter" Style="{StaticResource ButtonNavigationBarImageStyle}" />
                            
                        </StackLayout>
                        <StackLayout Style="{StaticResource ButtonNavigationBarStackLayoutStyle}" x:Name="stckshare">
                            <Image Margin="0,10,0,10" x:Name="imgshare" Style="{StaticResource ButtonNavigationBarImageStyle}" />
                            
                        </StackLayout>

                    </StackLayout>
                </StackLayout>
            </StackLayout>
            <ContentView x:Name="popupLoadingView" BackgroundColor="Transparent" Padding="10, 0" IsVisible="false"  AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
                <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
                    <StackLayout Orientation="Vertical" HeightRequest="150" WidthRequest="200" BackgroundColor="Transparent">

                        <ActivityIndicator x:Name="activityIndicator" Margin="0,50,0,0" VerticalOptions="Center" HorizontalOptions="Center" Color="Black" WidthRequest="40" HeightRequest="40" ></ActivityIndicator>
                    </StackLayout>
                </StackLayout>
            </ContentView>
        </AbsoluteLayout>
    </ContentPage.Content>
   
</ContentPage>

the registeredstdslist is a collection view that has data about students that i get from a database. registeredstdslist是一个集合视图,其中包含我从数据库中获取的有关学生的数据。 the code works well on android but on my ios simulator, it doesn't work.该代码在 android 上运行良好,但在我的 ios 模拟器上,它不起作用。 the collectionview shows no data when i write something in the searchbar though what i write has letters like some data in my list.当我在搜索栏中写一些东西时,collectionview 没有显示任何数据,尽管我写的东西有一些字母,比如我列表中的一些数据。 and when i erase what i wrote, all data are displayed again.当我删除我写的内容时,所有数据都会再次显示。 why is that?这是为什么? what am i doing wrong?我究竟做错了什么?

Update: i added this:更新:我添加了这个:

private void search_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (list_total_reg_std.Count != 0)
            {
                if (string.IsNullOrEmpty(e.NewTextValue))
                {
                    if (filtered == false)
                        registeredstdslist.ItemsSource = list_total_reg_std;
                    else
                        registeredstdslist.ItemsSource = list_filtered_reg_std;
                }

                else
                {
                    if (filtered == false)
                        if(list_total_reg_std.FindAll(x => x.name.ToLower().Contains(e.NewTextValue)).Count==0)
                            DisplayAlert("Operation Failed", "No matchiing data!", "Cancel");

                   // registeredstdslist.ItemsSource = list_total_reg_std.Where(x => x.name.ToLower().Contains(e.NewTextValue) || x.stdid.ToLower().Contains(e.NewTextValue) || x.edlevel.ToString().ToLower().Contains(e.NewTextValue));
                    else
                        registeredstdslist.ItemsSource = list_filtered_reg_std.Where(x => x.name.ToLower().Contains(e.NewTextValue) || x.stdid.ToLower().Contains(e.NewTextValue) || x.edlevel.ToString().ToLower().Contains(e.NewTextValue));

                }
            }

it actually gave No matching data but why?它实际上No matching data ,但为什么呢? and why does it work on android with no problems and on ios it doesn't?为什么它在 android 上没有问题而在 ios 上却没有?

One problem I see is that if you type any upper case, it will not match.我看到的一个问题是,如果您键入任何大写字母,它都不会匹配。

x.name.ToLower().Contains(e.NewTextValue) should be x.name.ToLower().Contains(e.NewTextValue.ToLower()) . x.name.ToLower().Contains(e.NewTextValue)应该是x.name.ToLower().Contains(e.NewTextValue.ToLower())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM