【问题标题】:sequence contains no elements in xamarin forms for listview序列不包含列表视图的 xamarin 表单中的元素
【发布时间】:2017-07-31 18:07:33
【问题描述】:

我正在尝试从数据中填充 ListView。 在我写的 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>

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

每次构建此项目时,我都会收到错误“序列不包含任何元素”。我已经看到一些在线教程具有相同的代码并且运行良好。这里出了什么问题?

【问题讨论】:

    标签: c# listview xamarin xamarin.forms


    【解决方案1】:

    您必须在布局中包含控件

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

    我已经写了这个little article关于这个......

    【讨论】:

      【解决方案2】:

      当您有一个带有绑定的 ListView 并且您忘记添加 Binding 关键字时,有时会发生这种情况

      例如:{Binding Name}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-03
        • 1970-01-01
        • 1970-01-01
        • 2019-06-30
        • 2021-08-06
        • 2022-08-19
        相关资源
        最近更新 更多