在Xamarin Forms中的click/tapped事件处理程序中传递对象 [英] Pass object in click/tapped event handler in Xamarin Forms

查看:363
本文介绍了在Xamarin Forms中的click/tapped事件处理程序中传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的工作类别:

public class Job
{
        public string Id{ get; set;}
        public string Name{ get; set;}
}

这是我的ListView:

And here is my 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?

您可以看到我向用户显示了一条消息.并且应该在其中放置被窃听对象的ID.

You can see that I show a message to the user. And in there it should stand the ID of the tapped object.

推荐答案

尝试:

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

这篇关于在Xamarin Forms中的click/tapped事件处理程序中传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆