[英]Xamarin forms ListView Search
我似乎无法正确更新它,它只显示列表的前 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();
}
在您的 SearchBar_TextChanged 中,我认为您没有以正确的方式提供 FlowListView 的来源。 而不是 productsListView.ItemsSource,它应该是:
productsListView.FlowItemsSource = x.Where(i => i.name.Contains(e.NewTextValue));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.