Pass object in click/tapped event handler in Xamarin Forms
Asked Answered
P

2

12

Here is my job class:

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

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?

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

Planar answered 11/3, 2015 at 11:34 Comment(3)
You can just use the object inside a lambda. It will create a closure which will save the reference to it.Symphonist
And the title is misleading, it's not an event, but rather event handler.Symphonist
@Symphonist I corrected the title. Thx.Planar
S
12

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
        };
Schuller answered 11/3, 2015 at 11:46 Comment(4)
It works!!!!!! What am I missing here? Does the sender contain the job object? And what are you doing? A cast? Can you reference me to an article, that explains it? Thanks a lot!Planar
this is a combination of different things. an EventHandler sends an object (o) and eventargs (e) in your case the sender o is a ListView therefore you must cast the object to a ListView to access it's properties. The property we need in this case is the SelectedItem property which is a Job and again we cast this (although I'm not sure we need to) and then access the Job's propertie Id this is an article explaining this (Boxing and Unboxing) If this answer has helped you please mark it as the correct answerSchuller
perfect explanation! no more to say herePlanar
@user1 You just saved me a ton of headaches. Thank you!Ironworker
E
6

The following page explains exactly what you are looking for: Selecting an Item in a 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);
};
Epineurium answered 17/3, 2015 at 6:52 Comment(1)
@Planar very happy to helpEpineurium

© 2022 - 2024 — McMap. All rights reserved.