[英]Pass object in click/tapped event handler in Xamarin Forms
Here is my job class: 这是我的工作班:
public class Job
{
public string Id{ get; set;}
public string Name{ get; set;}
}
And here is my ListView: 这是我的ListView:
public class JobListePage:ContentPage
{
// Members
private ListView lstView;
// Constructor
public JobListePage ()
{
// Set members
lstView = new ListView ();
// Create job objects
Job[] jobs = {new Job(){Id="1", Name="Benny"}, new Job(){Id="2", Name="Lukas"}};
// Fill listview with job objects
lstView.ItemsSource = jobs;
// HOW CAN I PASS THE TAPPED OBJECT HERE???
lstView.ItemTapped += async (o, e) => {
await DisplayAlert("Tapped", "HERE I WANT TO SHOW THE ID", "OK");
((ListView)o).SelectedItem = null; // de-select the row
};
....
Now how can I pass the tapped "job-object" to the event? 现在我如何将点击的“作业对象”传递给活动?
You can see that I show a message to the user. 您可以看到我向用户显示了一条消息。 And in there it should stand the ID of the tapped object. 在那里,它应该支持tapped对象的ID。
try: 尝试:
lstView.ItemTapped += async (o, e) => {
var myList= (ListView)o;
var myJob = (myList.SelectedItem as Job);
await DisplayAlert("Tapped", myJob.Id, "OK");
myList.SelectedItem = null; // de-select the row
};
The following page explains exactly what you are looking for: Selecting an Item in a ListView 以下页面准确说明了您要查找的内容: 在ListView中选择项目
listView.ItemSelected += async (sender, e) =>
{
await DisplayAlert("Tapped!", (e.SelectedItem as Job).Id + " was tapped.", "OK");
};
If you want to navigate to a detail page passing arguments then use the following: 如果要导航到传递参数的详细信息页面,请使用以下命令:
listView.ItemSelected += async (sender, e) =>
{
var jobPage = new JobPage(e.SelectedItem as Job); // new page shows correct data
await Navigation.PushAsync(jobPage);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.