这是我的工作班:
public class Job
{
public string Id{ get; set;}
public string Name{ get; set;}
}
这是我的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
};
....
现在我该怎样才能传递被点击的"作业对象"参加活动?
您可以看到我向用户显示了一条消息。在那里,它应该是窃听物体的ID。
答案 0 :(得分:9)
尝试:
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
};
答案 1 :(得分:6)
以下页面准确说明了您的目标:Selecting an Item in a ListView
listView.ItemSelected += async (sender, e) =>
{
await DisplayAlert("Tapped!", (e.SelectedItem as Job).Id + " was tapped.", "OK");
};
如果要导航到传递参数的详细信息页面,请使用以下命令:
listView.ItemSelected += async (sender, e) =>
{
var jobPage = new JobPage(e.SelectedItem as Job); // new page shows correct data
await Navigation.PushAsync(jobPage);
};