I am new to Xamarin... I have a problem when I want to show a description in a label. I have a listView with different items and when the user clicks in one item the app moves to a Tabbed Page with two main windows one for description and the other one for opinions. Here you have a diagram. photo
I pass the object from the item clicked to the Tabbed Page and from it I pass data to its children. Here is 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";
}
}
---> event handler to navigate to the TabPage once an item from listView is clicked.
private async void parent_listView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = (Item)e.SelectedItem;
//await Navigation.PushAsync(new TabPage(new Datos(itemList[e.SelectedItemIndex] ), new Opinions(itemList[e.SelectedItemIndex] )));
await Navigation.PushAsync(new TabPage(item));
}
----> How I get the data to the TabPage and send to its children.
public partial class TabPage : TabbedPage
{
Item item;
public TabPage(Item item)
{
InitializeComponent();
this.item = item;
inichildren(item);
}
private void inichildren(Item item)
{
Datos d = new Datos(item);
Opinions o = new Opinions(item);
}
}
---> Finally the Datos class where I am interested in.
public Datos()
{
InitializeComponent();
}
public Datos(Item i)
{
InitializeComponent();
lbl_description.Text = i.description.ToString();
lbl_item_price.Text = i.price.ToString();
}
Everything works fine and even I added some break point to see if I receive the data in Datos class and all labels variables seems to point to the right data. But the thing is that when I initialize the labels in the constructors no data appears on the screen.... I am new to Xamarin and don't have idea why this is happening. Many thanks for your help.
CodePudding user response:
if you want to pass data to the child pages via the constructor, you will need to setup the pages in code, not XAML.
private void inichildren(Item item)
{
Datos d = new Datos(item);
Opinions o = new Opinions(item);
// since you are adding the child pages here, remove them from the TabbedPage XAML
this.Children.Add(d);
this.Children.Add(o);
}