我是 Xamarin 的新手...當我想在標簽中顯示描述時遇到問題。我有一個包含不同專案的 listView,當用戶單擊一個專案時,應用程式會移動到一個帶有兩個主視窗的選項卡式頁面,一個用于描述,另一個用于意見。在這里你有一個圖表。照片
我將物件從單擊的專案傳遞到選項卡式頁面,然后將資料傳遞給其子項。這是代碼
---> 型號
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";
}
}
---> 單擊 listView 中的專案后導航到 TabPage 的事件處理程式。
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));
}
----> 我如何將資料獲取到 TabPage 并發送給它的孩子。
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);
}
}
---> 最后是我感興趣的 Datos 課程。
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.
uj5u.com熱心網友回復:
如果要通過建構式將資料傳遞到子頁面,則需要在代碼中設定頁面,而不是 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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/399137.html
