Xamarin.Forms ListView 单击下一页

本文关键字:一页 单击 ListView Forms Xamarin | 更新日期: 2023-09-27 17:56:52

当我单击特定列表项时,我无法将值(id)传递到下一页。

我的 Xaml 文件

 <ListView  x:Name="Clublistview" HasUnevenRows="true" ItemSelected="OnItemSelected">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell Height="50">
                            <StackLayout BackgroundColor="White"
                            Orientation="Vertical">
                                <StackLayout Orientation="Horizontal">
                                    <Image Source="{Binding Logo}" IsVisible="true" WidthRequest="50" HeightRequest="50"/>
                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

我的.CS文件

async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        ListView btn = (ListView)sender;
        int clubid = (int)btn.CommandParameter;
        await Navigation.PushAsync(new DetailedClub(clubid));
    }

Xamarin.Forms ListView 单击下一页

您没有将 CommandParameter 绑定到任何内容。 更简单的方法是

async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    ListView lv = (ListView)sender;
    // this assumes your List is bound to a List<Club>
    Club club = (Club) lv.SelectedItem;
    // assuiming Club has an Id property
    await Navigation.PushAsync(new DetailedClub(club.Id));
}