简体   繁体   English

如何从Listview Xamarin.form获取值

[英]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. 我想从我的网络服务的clubid中获取价值,并将其传递到下一页,这样我就可以获得动态的俱乐部详细信息。 However, when I tried to do it, it shows me some errors. 但是,当我尝试执行此操作时,它显示了一些错误。 This are my codes. 这是我的代码。

ListClubs.xaml.cs 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 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". 使用名称“ lbId”的作用不大,因为列表中的每个项目都有一个名为“ lbId”的Label

You can use CommandParameter to add the Id to your Button 您可以使用CommandParameter将ID添加到您的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));
}

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

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