如何在 Xamarin 中进行过滤

How to filter in Xamarin

我对 Xamarin 很陌生,我想在 xamarin 中进行过滤。所以我有三个按钮饮料、食物和所有。当它全部显示时,我想显示所有这些,当点击饮料按钮时,我只想显示饮料等。

这是我的按钮:

<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
                    <Button Text="All"  x:Name="All" Clicked="Handle_Filter" TextColor="#EEF2FF" FontSize="18" HorizontalOptions="Center" BackgroundColor="#FF5959" />
                </StackLayout>
                <StackLayout Grid.Column="1" HorizontalOptions="Center" VerticalOptions="Center">
                    <Button Text="Food"  x:Name="Food" Clicked="Handle_Filter" TextColor="#676FA3" FontSize="18" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
                </StackLayout>
                <StackLayout Grid.Column="2" HorizontalOptions="Center" VerticalOptions="Center">
                    <Button Text="Drinks"  x:Name="Drinks" Clicked="Handle_Filter" TextColor="#676FA3" FontSize="18" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
                </StackLayout>

以及需要过滤的区域:

<CollectionView Grid.Row="3" Margin="25" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
                        SelectionMode="None" ItemsSource="{Binding AllProducts}">
            <CollectionView.ItemsLayout>
                <LinearItemsLayout Orientation="Vertical" ItemSpacing="20"/>
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <pv:PancakeView HasShadow="True" BackgroundColor="#EEF2FF" VerticalOptions="StartAndExpand" 
                                    HorizontalOptions="FillAndExpand">
                        <Grid ColumnSpacing="3" HorizontalOptions="FillAndExpand">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="50"/>
                            </Grid.RowDefinitions>
                            <BoxView Grid.RowSpan="1" BackgroundColor="#EEF2FF"/>
                            <BoxView Grid.Column="1" Grid.RowSpan="1" BackgroundColor="#EEF2FF"/>
                            <BoxView Grid.Column="2" Grid.RowSpan="1" BackgroundColor="#EEF2FF"/>
                            <Button
                                Text="{Binding Name}"
                                Grid.Column="0"
                               HorizontalOptions="Center"
                               VerticalOptions="Center"
                                TextColor="Black"
                                 BackgroundColor="#EEF2FF"
                                />
                        </Grid>
                    </pv:PancakeView>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

我的产品:

public class Product
{
    public string Type { get; set; }
    public string Name { get; set; }
    public int Stock { get; set; }
}

非常感谢您的帮助,因为我是 Xamarin 的新手。 谢谢

您可以使用 LINQ 查询来做到这一点

protected void FilterFood(object sender, EventArgs args)
{
  myCollectionView.ItemsSource = AllProducts.Where(x => x.Type == "Food").ToList();
}

您在 Xaml 文件中的按钮:

 <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
                        <Button Text="All"  x:Name="All" Clicked="onAllFilterClicked" TextColor="#EEF2FF" FontSize="18" HorizontalOptions="Center" BackgroundColor="#FF5959" />
                    </StackLayout>
                    <StackLayout Grid.Column="1" HorizontalOptions="Center" VerticalOptions="Center">
                        <Button Text="Food"  x:Name="Food" Clicked="onFoodFilterClicked" TextColor="#676FA3" FontSize="18" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
                    </StackLayout>
                    <StackLayout Grid.Column="2" HorizontalOptions="Center" VerticalOptions="Center">
                        <Button Text="Drinks"  x:Name="Drinks" Clicked="onDrinksFilterClicked" TextColor="#676FA3" FontSize="18" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
    </StackLayout>

为您的 CollectionView 设置一个名称x:Name="cvProducts"

在您的 Buttons .cs 文件中:

protected void onAllFilterClicked(object sender, EventArgs args)
{
  cvProducts.ItemsSource = AllProducts;
}

protected void onFoodFilterClicked(object sender, EventArgs args)
{
  cvProducts.ItemsSource = AllProducts.Select(x => x.Type == "Food").ToList();
}

protected void onDrinksFilterClicked(object sender, EventArgs args)
{
  cvProducts.ItemsSource = AllProducts.Select(x => x.Type == "Drinks").ToList();
}