获取点击的集合视图项xamarin的索引

xvw2m8pv  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(129)

我有绑定到代理数据的CollectionView(来自Valorant-api)

private async void ShowAgents()
    {
        Root agent = await AgentRepository.GetAgent();
        Agents = new ObservableCollection<Data>(agent.data);

        lvwAgents.ItemsSource = Agents;
    }

xaml:

<CollectionView
            x:Name="lvwAgents"
            Grid.Row="5"
            Grid.ColumnSpan="2"
            HorizontalScrollBarVisibility="Never"
            Margin="0,-25,-75,0">
            
            <CollectionView.ItemsLayout>
                <LinearItemsLayout ItemSpacing="10" Orientation="Horizontal" />
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate >
                <DataTemplate>
                    <Grid>
                        <pcview:PancakeView 
                        CornerRadius="60,60,60,60"
                        HeightRequest="200" 
                        BackgroundGradientStartPoint="0,0" 
                        BackgroundGradientEndPoint="1,0"
                        VerticalOptions="End">
                            <pcview:PancakeView.BackgroundGradientStops>
                                <pcview:GradientStopCollection>
                                    <pcview:GradientStop Color="{Binding backgroundGradientColors[0]}" Offset="0" />
                                    <pcview:GradientStop Color="{Binding backgroundGradientColors[1]}" Offset="0.75" />
                                </pcview:GradientStopCollection>
                            </pcview:PancakeView.BackgroundGradientStops>
                            <StackLayout>
                                <Label Text="{Binding displayName}" Padding="30,75,0,0" FontFamily="GilroyExtraBold" TextColor="White" VerticalOptions="Center" FontSize="30"/>
                                <Label Text="Click to check agent info" Padding="30,0,40,0" FontFamily="FontCamptonMedium" TextColor="White" VerticalOptions="Center" FontSize="20"/>

                            </StackLayout>
                        </pcview:PancakeView>

                        <Image x:Name="AgentImage" Source="{Binding fullPortrait}" 
                            Margin="0,5,0,0"
                            HeightRequest="300"
                            HorizontalOptions="Center"
                            VerticalOptions="Start"
                            WidthRequest="300"/>
                        <Grid.GestureRecognizers>
                            <TapGestureRecognizer
                                Tapped="OnTapGestureRecognizerTapped"
                                NumberOfTapsRequired="1" />
                        </Grid.GestureRecognizers>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

点击是用"OnTapGestureRecognizerTapped"函数处理的,但我需要点击项的索引,但不知道如何处理。
拍子处理程序:

async void OnTapGestureRecognizerTapped(object sender, EventArgs args)
    {
        await ExecuteNavigateToDetailPageCommand(Agents);
    }
   
    private async Task ExecuteNavigateToDetailPageCommand(ObservableCollection<Data> data)
    {
        await Navigation.PushAsync(new ValorantApp.Views.DetailPage(data));
    }

我已经到处搜索了,但没有找到答案。我想做的是,当点击CollectionView中的某个项目时,我将获得该项目的索引。
任何帮助都是非常感谢的。谢谢!

brccelvz

brccelvz1#

您可以从BindingContext取得目前列的项目

async void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
    // I *think* the sender will be the Grid, but you 
    // might have to verify in the debugger.  If I'm 
    // wrong, cast to whatever the correct type is
    var grid = (Grid)sender;

    // item will be the Data corresponding to the row
    // you tapped.  If you actually need the index,
    // you can use IndexOf() to find it
    var item = (Data)grid.BindingContext;

    await ExecuteNavigateToDetailPageCommand(Agents);
}

相关问题