Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequence contains no elements in xamarin forms for listview

I am trying to populate a ListView from a data. In my XAML file I have written

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="FormDemos.Views.Feedback">
    <ContentPage.Content>
        <Label>I am Testimonials Page</Label>

        <ListView x:Name="FeedbackList" />

    </ContentPage.Content>
</ContentPage>

In CS file

namespace FormDemos.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Feedback : ContentPage
    {
        class City
        {
            public string Name
            {
                get;
                set;
            }

            public string State
            {
                get;
                set;
            }
        }

        public Feedback()
        {
            InitializeComponent();
            var cities = new List<City>() {
                new City() {State = "FL", Name = "Miami"},
                new City() {State = "CA", Name = "San Francisco"},
                new City() {State = "CA", Name = "Los Angeles"},
                new City() {State = "FL", Name = "Orlando"},
                new City() {State = "TX", Name = "Houston"},
                new City() {State = "NY", Name = "New York City"},
            };
            FeedbackList.ItemsSource = cities;
        }
    }
}

Everytime I build this project I get Error "Sequence contains no elements". I have seen some online tutorials have the same code and running well. What's going wrong here?

like image 956
BlackHat Avatar asked Mar 10 '17 11:03

BlackHat


2 Answers

You have to include controls inside a layout

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="FormDemos.Views.Feedback">
    <ContentPage.Content>
      <StackLayout>
        <Label>I am Testimonials Page</Label>

        <ListView x:Name="FeedbackList" />
      </StackLayout>
    </ContentPage.Content>
</ContentPage>

I have written this little article about this...

like image 92
Alessandro Caliaro Avatar answered Oct 25 '22 06:10

Alessandro Caliaro


Sometimes this happens when you have a ListView with Bindings and when you forget to add Binding keyword

Eg: {Binding Name}

like image 40
Pabodha Wimalasuriya Avatar answered Oct 25 '22 08:10

Pabodha Wimalasuriya