Xamarin Forms - проблема выбора ListView - PullRequest
0 голосов
/ 05 августа 2020

У меня базовый c 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>

Когда я выбираю элемент, я хочу передать выбранный продукт на другую страницу «ProductDetailPage».

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

Проблема в том, что кажется, что он переносится на всю историю отбора. Например, вот несколько тестовых случаев:

Выбор: Яблоко - Выход: Яблоко Выбор: Груша - Выход: Яблоко, Яблоко, Выбор груши: Виноград - Выход: Яблоко, Яблоко, Груша, Виноград

Надеюсь, это имеет смысл.

Я также пробовал очистить выделение, но когда он очищается, отображается пустая страница.

((ListView)sender).SelectedItem = null;

Надеюсь, у вас есть решение для этого.

...