我對 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 的新手,因此我們將不勝感激。謝謝
uj5u.com熱心網友回復:
您可以使用 LINQ 查詢來執行此操作
protected void FilterFood(object sender, EventArgs args)
{
myCollectionView.ItemsSource = AllProducts.Where(x => x.Type == "Food").ToList();
}
uj5u.com熱心網友回復:
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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446857.html
標籤:xamarin xamarin.forms xamarin.android xamarin.ios xamarin 工作室
