Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

ListView element is not populated in case if it's placed on non-root page

$
0
0

Hello!

I'm trying to add a ListView populated with some data.

I have some initial page which is a NagivationPage. This page leads to another page with ListView using Navigation.PushAsync.

ListView control is added right as it's done in TodoPCL example, the only difference is in the data population way - I'm not using an SQL, but a hardcoded array instead of it.

The problem is - I don't see any ListView (or at least items in it) on the page.

Here are some simplified code snippets:

    public class App : Application
        {
            public App()
            {
                MainPage = new NavigationPage(new MainPage());
            }

            ...
        }

//

    public class MainPage : ContentPage
        {
            public MainPage()
            {
                var startButton = new Button { Text = "Start" };
                var homePage = new HomePage();
                startButton.Clicked += (sender, e) => { this.Navigation.PushAsync(homePage); };
                Content = new StackLayout {  Children = { startButton } };
            }
        }

//

        public class HomePage : ContentPage
            {
                ListView listView;
                public HomePage()
                {
                    listView = new ListView { ItemTemplate = new DataTemplate(typeof(ProductItemCellView)) };
                    Content = new StackLayout { Children = { listView } };
                }

                protected override void OnAppearing()
                {
                    base.OnAppearing();
                    var productsCollection = new ProductItem[]
                                                 {
                                                     new ProductItem {
                                                             Name = "Some product",
                                                             Description = "Super nice product",
                                                             Image = new Image() },
                                                     new ProductItem {
                                                             Name = "Another product",
                                                             Description = "Another super nice product",
                                                             Image = new Image() }
                                                 };
                    listView.ItemsSource = productsCollection;
                }

//

    public class ProductItemCellView : ViewCell
    {
        public ProductItemCellView()
        {
            var productName = new Label();
            productName.SetBinding(Label.TextProperty, "Name");

            var productDescription = new Label();
            productDescription.SetBinding(Label.TextProperty, "Description");

            var layout = new StackLayout { Children = { productName, productDescription } };
            View = layout;
        }

        protected override void OnBindingContextChanged()
        {
            View.BindingContext = BindingContext;
            base.OnBindingContextChanged();
        }
    }

I get no exceptions, the HomePage is loaded just fine, but in the example above there's no items in the ListView and after I put it directly to App instead of MainPage - I can see ListView populated with data. I use Xamarin Forms 1.3.0.

What am I doing wrong?

Many Thanks!

Best Regard,
Vladimir


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>