传递的数据未显示在listView中

时间:2017-12-15 08:42:54

标签: c# xamarin.forms

我正在尝试将数据从子页面传递到父类,这是父类

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;
        }
    }
}

这是儿童班......

<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>

我基本上需要接收数据,现在当我调试程序时,信息通过完美传递但是当我填充到列表视图时它不会显示我运行应用程序时?

任何帮助都会很棒,谢谢

1 个答案:

答案 0 :(得分:1)

这可能是罪魁祸首:elements = new ObservableCollection<ElementsViewModel>();

每次都不要新ObservableCollection。这将导致绑定更新UI,您的更改将不会显示。只需实例化elements一次:public ObservableCollection<ElementsViewModel> elements { get; set; } = new ObservableCollection<ElementsViewModel>();并在每次需要空的时清除它。然后只需逐个添加项目。

这里有用的包是James Montemagno的MVVM Helpers。它有ObservableRangeCollection,可让您替换整个范围等,为您节省一些时间。