简体   繁体   English

Xamarin Forms - ListView 选择问题

[英]Xamarin Forms - ListView Selection Issue

I have a basic ListView here:我在这里有一个基本的 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>

When I select an item, I want to pass the product selected to another page "ProductDetailPage".当我 select 一个项目时,我想将选择的产品传递到另一个页面“ProductDetailPage”。

async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item = (Product)e.SelectedItem;
            await Navigation.PushAsync(new ProductDetailPage(item));
        }

The problem is that it seems to carry over the entire history of selection.问题是它似乎延续了整个选择的历史。 For example here's a few test cases:例如这里有几个测试用例:

Selection: Apple - Output: Apple Selection: Pear - Output: Apple, Apple, Pear Selection: Grape - Output: Apple, Apple, Pear, Grape选择:苹果 - Output:苹果 选择:梨 - Output:苹果,苹果,梨 选择:葡萄 - Output:苹果,苹果

Hope that makes sense.希望这是有道理的。

I also tried clearing the selection but when it clears, it shows a blank page.我也尝试清除选择,但是当它清除时,它显示一个空白页。

((ListView)sender).SelectedItem = null;

Hope you have a solution for this.希望您对此有解决方案。

I was able to fix the issue.我能够解决这个问题。 If anyone wants to know, you need to store the selected product details inside a global variable and when you add the item to cart, simply retrieve the selected product variable.如果有人想知道,您需要将所选产品详细信息存储在全局变量中,当您将项目添加到购物车时,只需检索所选产品变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM