简体   繁体   English

对于listview,序列不包含xamarin形式的元素

[英]sequence contains no elements in xamarin forms for listview

I am trying to populate a ListView from a data. 我试图从数据填充ListView。 In my XAML file I have written 在我写的XAML文件中

<?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 在CS文件中

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? 这里出了什么问题?

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... 我写过关于这个的小文章 ......

Sometimes this happens when you have a ListView with Bindings and when you forget to add Binding keyword 有时当你有一个带有Bindings的ListView并且忘记添加Binding关键字时会发生这种情况

Eg: {Binding Name} 例如: {Binding Name}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM