[英]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='${0}'}" TextColor="White" HorizontalTextAlignment="Center"></Label>
</Frame>
</StackLayout>
</Frame>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
當我 select 一個項目時,我想將選擇的產品傳遞到另一個頁面“ProductDetailPage”。
async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = (Product)e.SelectedItem;
await Navigation.PushAsync(new ProductDetailPage(item));
}
問題是它似乎延續了整個選擇的歷史。 例如這里有幾個測試用例:
選擇:蘋果 - Output:蘋果 選擇:梨 - Output:蘋果,蘋果,梨 選擇:葡萄 - Output:蘋果,蘋果
希望這是有道理的。
我也嘗試清除選擇,但是當它清除時,它顯示一個空白頁。
((ListView)sender).SelectedItem = null;
希望您對此有解決方案。
我能夠解決這個問題。 如果有人想知道,您需要將所選產品詳細信息存儲在全局變量中,當您將項目添加到購物車時,只需檢索所選產品變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.