【问题标题】:Xamarin forms ListView SearchXamarin 表单 ListView 搜索
【发布时间】:2020-01-22 11:50:40
【问题描述】:

我似乎无法正确更新它,它只显示列表的前 2 项。

我正在从 Web API 接收 json 数据,我只是希望能够在列表视图中搜索名称并显示它,我已经尝试了很多这样的示例但似乎无法得到它要真正起作用,我是否需要将列表封装并订阅到 INotify?

  <StackLayout>

      <SearchBar TextChanged="SearchBar_TextChanged"></SearchBar>   
        <FlexLayout  Wrap="Wrap" Direction="Row" JustifyContent="SpaceEvenly" >
            <flv:FlowListView x:Name="productsListView"

                     FlowColumnCount="2" HasUnevenRows="false" RowHeight="320"
                  VerticalOptions="FillAndExpand"
                      SeparatorVisibility="None"
                      SelectedItem="{Binding Name}"
                      >

                <flv:FlowListView.FlowColumnTemplate>
                    <DataTemplate>
                        <Grid Padding="5">
                            <Frame BorderColor="#f39000" HeightRequest="310" >
                                <StackLayout   Orientation="Vertical" Spacing="5">

                                    <Label HorizontalTextAlignment="Center" FontSize="Medium" FontAttributes="Bold" TextColor="Black" x:Name="idlabel" Text="{Binding name}">

                                    </Label>
                                    <ffimageloading:CachedImage HorizontalOptions="Center" VerticalOptions="Center"
                                            WidthRequest="150" HeightRequest="150"
                                            DownsampleToViewSize="true"
                                            Source = "{Binding description}">
                                    </ffimageloading:CachedImage>



                                    <Button Text="See Products" 
                                            Padding="5"
                                            FontSize="Small"
                                            BorderColor="#f39000"
                                            BackgroundColor="#f39000"
                                            CornerRadius="10"
                                            TextColor="White"
                                            VerticalOptions="CenterAndExpand"
                                            HorizontalOptions="Center"
                                            Clicked="SupplierClicked" 
                                            BindingContext="{Binding id}"/>
                                </StackLayout>
                            </Frame>
                        </Grid>
                    </DataTemplate>
                </flv:FlowListView.FlowColumnTemplate>
            </flv:FlowListView>
        </FlexLayout>
      </StackLayout>
public List<ProductTag> x;
public async Task InitAsync()
{

    var p = await wc.Tag.GetAll(new Dictionary<string, string>() {

                   { "per_page", "100" } });

                x = p;
                productsListView.FlowItemsSource = p;
}

 private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
        {
            //thats all you need to make a search

            productsListView.BeginRefresh();

            if (string.IsNullOrWhiteSpace(e.NewTextValue))
                productsListView.ItemsSource = x;
            else
                productsListView.ItemsSource = x.Where(i => i.name.Contains(e.NewTextValue));

            productsListView.EndRefresh();
}

【问题讨论】:

    标签: c# json xamarin.forms


    【解决方案1】:

    在您的 SearchBar_TextChanged 中,我认为您没有以正确的方式提供 FlowListView 的来源。而不是 productsListView.ItemsSource,它应该是:

    productsListView.FlowItemsSource = x.Where(i => i.name.Contains(e.NewTextValue));
    

    【讨论】:

    • uhhg,我经常这样做。是的,这是正确的,在我的情况下,我需要投射到一个列表中,所以productsListView.FlowItemsSource = x.Where(i =&gt; i.name.Contains(e.NewTextValue)).toList();
    猜你喜欢
    • 2017-11-12
    • 2016-03-26
    • 2023-03-10
    • 2018-03-11
    • 1970-01-01
    • 2018-10-14
    • 2016-10-10
    • 2018-03-08
    • 1970-01-01
    相关资源
    最近更新 更多