C# 传递的数据未显示在listView中
我试图将数据从子页面传递到父类这是父类C# 传递的数据未显示在listView中,c#,xamarin.forms,C#,Xamarin.forms,我试图将数据从子页面传递到父类这是父类 namespace App11 { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class OrderedItemsPage : ContentPage { public string aSize; public string size, Main, Milk; public List<Stri
namespace App11
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class OrderedItemsPage : ContentPage
{
public string aSize;
public string size, Main, Milk;
public List<String> sizeList = new List<string>();
public List<String> mainList = new List<string>();
public List<String> milkList = new List<string>();
public ObservableCollection<ElementsViewModel> elements { get; set; }
public void OrderedItemsPageBlank()
{
aSize = "";
Main = "";
Milk = "";
}
public OrderedItemsPage ()
{
InitializeComponent ();
}
public void displayToList()
{
elements = new ObservableCollection<ElementsViewModel>();
for(int i = 0;i< mainList.Count;i++)
{
string Coffee = sizeList[i] + " : " + mainList[i];
elements.Add(new ElementsViewModel
{
Image = "icon.png",
Coffee = Coffee,
Details = "$2.50 each"
});
}
listView.ItemsSource = elements;
}
}
}
名称空间App11
{
[XamlCompilation(XamlCompilationOptions.Compile)]
公共分部类OrderedItemsPage:ContentPage
{
公共字符串aSize;
公串大小、主、乳;
public List sizeList=新列表();
public List mainList=新列表();
public List milkList=新列表();
公共可观测集合元素{get;set;}
public void OrderedItemsPageBlank()
{
aSize=“”;
Main=“”;
牛奶=”;
}
公共秩序编辑邮件()
{
初始化组件();
}
公共无效显示列表()
{
元素=新的ObservableCollection();
对于(int i=0;i
这是儿童班
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App11.OrderedItemsPage">
<ContentPage.Content>
<StackLayout >
<!--Top Section-->
<ListView x:Name="listView" RowHeight="60">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout
Orientation="Horizontal"
HorizontalOptions="Fill">
<Image x:Name="BgImage" Source="{Binding Image}"
AbsoluteLayout.LayoutBounds="250.25, 0.25, 50, 50"/>
<StackLayout Orientation="Vertical">
<Label Text="{Binding Coffee}"
FontSize="24"
AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"
></Label>
<Label Text="{Binding Details}"
AbsoluteLayout.LayoutBounds="50, 50, 200, 25"
></Label>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackLayout Orientation="Horizontal">
<!--Second half-->
<Button
x:Name="AddButton"
Text="Add Drink"
VerticalOptions="EndAndExpand"
HorizontalOptions="CenterAndExpand"
Clicked="addDrinkButtonClick">
</Button>
<Button Text="Add Food"
VerticalOptions="EndAndExpand"
HorizontalOptions="CenterAndExpand">
</Button>
</StackLayout>
<!--third half ( cause thats a thing)-->
<StackLayout Orientation="Vertical">
<Button Text="Process Order"
VerticalOptions="EndAndExpand"
HorizontalOptions="CenterAndExpand"
></Button>
</StackLayout>
</StackLayout>
</ContentPage.Content>
我基本上需要接收数据,现在当我调试程序时,信息被完美地传递,但是当我填充到列表视图中时,它在我运行应用程序时不会显示
任何帮助都会很棒,谢谢这可能是罪魁祸首:
elements=newobservetecollection()代码>
不要每次都新建一个可观察到的集合
。这将导致绑定更新UI,而您的更改将不会显示。只需实例化元素一次:公共ObservableCollection元素{get;set;}=newObservableCollection()代码>并在每次需要空代码时将其清除。然后一个接一个地添加项目
詹姆斯·蒙特马诺(James Montemagno)的一个有用的软件包。它有ObserverAgeCollection
,可以替换整个范围,这样可以省去一些麻烦。您可能需要研究使用MVVM体系结构,请参阅