Home > other >  Pass data from listView to tabPage in xamarin forms
Pass data from listView to tabPage in xamarin forms

Time:12-31

I am new to xamarin forms and I am trying to make a project similar to what you can see in the next picture. photo

The thing I want to achieve is when I press an item from listView (which I already have with the items displayed) where I have all items I want to pass somehow the entire object to the tabPage so that I can see the description in one part and the opinions in the other page. I did some research but all the examples I saw uses MVVM and mine is more simple than that. Here you have the code.

--->Model

public class Item
{
    public String name;
    public String description;
    public Double price;
    public int stock;
    public List<String> opinions;

    public Item(String name, String description ,Double price, int stock, List<String> opinions)
    {
        this.name = name;
        this.description = description;
        this.price = price;
        this.stock = stock;
        this.opinions = opinions;
    }
    public override string ToString()
    {
        return this.name   " "   this.price   " $"   this.stock   " ud";
    }
}

---> The event that takes me to the TabPage.

 private async void parent_listView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        //here maybe I should pass the object...
        await Navigation.PushAsync(new TabPage());
    }

---> Here you have both classes that are used inside the tabPage

 public partial class Datos : ContentPage
{
    //private string descriptionList;
    public Datos()
    {
        InitializeComponent();
        
    }
    
}


public partial class Opinions : ContentPage
{
    public Opinions()
    {
        InitializeComponent();
    }
   
}

The thing is that I don't know how to pass the object in a specific position from listView to both pages from tabPage in a easy way. I tried to pass both classes(Datos and Opinions) using TabPage constructor but it doesn't work. Here you have the code...

private async void parent_listView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        
        await Navigation.PushAsync(new TabPage(new Datos(itemList[e.SelectedItemIndex] ), new Opinions(itemList[e.SelectedItemIndex] )));
    }

CodePudding user response:

SelectedItemChangedEventArgs contains a reference to the selected object

private async void parent_listView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    var item = (Item)e.SelectedItem;

    await Navigation.PushAsync(new TabPage(item));
}
  • Related