[英]Is there any way to have a double click event on Listview and send list data to another page in Xamarin Forms
This is what I have done now that I am unable to send ID
to another page.这就是我现在所做的,因为我无法将ID
发送到另一个页面。 And there's an error at Command Parameter as null reference exception.命令参数出现错误,为 null 引用异常。
Xaml: Xaml:
<ListView x:Name="studentListView" ItemsSource="{Binding .} ">
<ListView.ItemTemplate >
<DataTemplate >
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding StudentID}"></Label>
<Label Text="{Binding FirstName}"></Label>
<Label Text="{Binding LastName}"></Label>
<Label Text="{Binding Gender}"></Label>
<Label Text="{Binding Age}"></Label>
<Label Text="{Binding Class}"></Label>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="TapGestureRecognizer_Tapped" CommandParameter="{Binding StudentID}"/>
<TapGestureRecognizer NumberOfTapsRequired="2" Tapped="TapGestureRecognizer_Tapped_1"/>
</StackLayout.GestureRecognizers>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C# Code below: C# 验证码如下:
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
TapGestureRecognizer tapGesture = sender as TapGestureRecognizer;
int clubid = (int) tapGesture.CommandParameter;
Navigation.PushAsync(new StudentPage(clubid));
}
You could also directly add a TapGestureRecognizer
on the StudentID of the Label
and then get the ID value to send it to another Page.也可以直接在Label
的StudentID上添加一个TapGestureRecognizer
,然后获取ID值发送到另一个Page。 Here's the code below for your reference:以下是供您参考的代码:
Student Page Xaml:学生页面 Xaml:
<ListView x:Name="studentListView" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding StudentID}" WidthRequest="100" >
<Label.GestureRecognizers >
<TapGestureRecognizer NumberOfTapsRequired="2" Tapped="TapGestureRecognizer_Tapped">
</TapGestureRecognizer>
</Label.GestureRecognizers>
</Label>
<Label Text="{Binding FirstName}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code in Back-end:后端代码:
public StudentList()
{
InitializeComponent();
studentListView.ItemsSource = new List<MyStudent>
{
new MyStudent{StudentID="1", FirstName="Steve"},
new MyStudent{StudentID="2", FirstName="Lorence"}
};
}
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
//get the StudentID and pass it through constructor
var s = (sender as Label).Text;
Navigation.PushModalAsync(new MainPage(s));
}
Then, get the StudentID
via Constructor in another page.然后,在另一个页面通过Constructor获取StudentID
。
public MainPage(string a)
{
InitializeComponent();
App.Current.MainPage.DisplayAlert("Student Id is", a, "OK");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.