Xamarin Forms - ListView 选择问题
Xamarin Forms - ListView Selection Issue
我这里有一个基本的 ListView:
<ListView x:Name="productsListView"
HasUnevenRows="True"
VerticalOptions="FillAndExpand"
SeparatorVisibility="None"
ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Frame HasShadow="True" Padding="20" Margin="20">
<StackLayout>
<Image Source="{Binding featured_src}"/>
<Label Text="{Binding title}" FontSize="Medium"/>
<Frame BackgroundColor="Red" Padding="5" HorizontalOptions="Center" WidthRequest="80" HeightRequest="20" CornerRadius="00">
<Label WidthRequest="40" Text="{Binding price , StringFormat='[=10=]'}" TextColor="White" HorizontalTextAlignment="Center"></Label>
</Frame>
</StackLayout>
</Frame>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
当我 select 一个项目时,我想将产品 select 传递到另一个页面“ProductDetailPage”。
async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = (Product)e.SelectedItem;
await Navigation.PushAsync(new ProductDetailPage(item));
}
问题是它似乎继承了 selection 的整个历史。例如这里有几个测试用例:
选择:Apple - 输出:Apple
选择:梨 - 输出:Apple, Apple, Pear
选择:葡萄 - 输出:Apple, Apple, Pear, Grape
希望这是有道理的。
我也尝试清除 selection,但是清除后显示空白页。
((ListView)sender).SelectedItem = null;
希望您对此有解决方案。
我能够解决这个问题。如果有人想知道,您需要将所选产品的详细信息存储在全局变量中,当您将商品添加到购物车时,只需检索所选产品变量即可。
我这里有一个基本的 ListView:
<ListView x:Name="productsListView"
HasUnevenRows="True"
VerticalOptions="FillAndExpand"
SeparatorVisibility="None"
ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Frame HasShadow="True" Padding="20" Margin="20">
<StackLayout>
<Image Source="{Binding featured_src}"/>
<Label Text="{Binding title}" FontSize="Medium"/>
<Frame BackgroundColor="Red" Padding="5" HorizontalOptions="Center" WidthRequest="80" HeightRequest="20" CornerRadius="00">
<Label WidthRequest="40" Text="{Binding price , StringFormat='[=10=]'}" TextColor="White" HorizontalTextAlignment="Center"></Label>
</Frame>
</StackLayout>
</Frame>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
当我 select 一个项目时,我想将产品 select 传递到另一个页面“ProductDetailPage”。
async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = (Product)e.SelectedItem;
await Navigation.PushAsync(new ProductDetailPage(item));
}
问题是它似乎继承了 selection 的整个历史。例如这里有几个测试用例:
选择:Apple - 输出:Apple 选择:梨 - 输出:Apple, Apple, Pear 选择:葡萄 - 输出:Apple, Apple, Pear, Grape
希望这是有道理的。
我也尝试清除 selection,但是清除后显示空白页。
((ListView)sender).SelectedItem = null;
希望您对此有解决方案。
我能够解决这个问题。如果有人想知道,您需要将所选产品的详细信息存储在全局变量中,当您将商品添加到购物车时,只需检索所选产品变量即可。