简体   繁体   中英

How to get the value from Listview Xamarin.form

I want to get the value from my clubid that I web service and pass it down to the next page so I would have a dynamic club detail. However, when I tried to do it, it shows me some errors. This are my codes.

ListClubs.xaml.cs

ClubApiClient service = new ClubApiClient();

public ListClubs ()
{
    InitializeComponent();
}

protected async override void OnAppearing()
{
    base.OnAppearing();
    await setClubs(Clublistview);
}

private async Task setClubs(ListView listview)
{
    var clublist = await service.getClubList(2);
    listview.ItemsSource = clublist.ToList();
}

async void OnListClubsClicked(object sender, EventArgs e)
{
    int clubid = int.Parse(lbId.Text); //Error msg: The name 'lbId' does not exist in the context
    await Navigation.PushAsync(new DetailedClub());
}

ListClubs.xaml

<ListView  x:Name="Clublistview">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout BackgroundColor="White"
                    Orientation="Vertical">
                        <StackLayout Orientation="Horizontal">
                            <Image Source="{Binding Logo}" IsVisible="true" WidthRequest="42" HeightRequest="42"/>
                             <Button Text="{Binding Name}" x:Name="BtnClub" AbsoluteLayout.LayoutBounds="100, 25, 100, 25" Clicked="OnListClubsClicked" TextColor="Black" VerticalOptions="FillAndExpand"/>
                             <Label Text="{Binding Id}" x:Name="lbId" IsVisible="false"/>
                        </StackLayout>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

using the name "lbId" isn't useful because every item in your list has a Label named "lbId".

You can use CommandParameter to add the Id to your Button

<Button Text="{Binding Name}" x:Name="BtnClub" AbsoluteLayout.LayoutBounds="100, 25, 100, 25" Clicked="OnListClubsClicked" TextColor="Black" VerticalOptions="FillAndExpand" CommandParameter="{Binding Id}" />

async void OnListClubsClicked(object sender, EventArgs e)
{
    Button btn = (Button) sender;
    int clubid = (int) btn.CommandParameter;
    // pass the clubid to your Detail page
    await Navigation.PushAsync(new DetailedClub(clubid));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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