gpt4 book ai didi

c# - 在 Xamarin Forms 的点击/点击事件处理程序中传递对象

转载 作者:太空狗 更新时间:2023-10-29 17:39:06 26 4
gpt4 key购买 nike

这是我的工作类:

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。

最佳答案

尝试:

        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
};

关于c# - 在 Xamarin Forms 的点击/点击事件处理程序中传递对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28985571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com