c# - 序列不包含 ListView 的 xamarin 表单中的元素

标签 c# listview xamarin xamarin.forms

我正在尝试从数据填充 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;
        }
    }
}

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

最佳答案

你必须在布局中包含控件

<?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关于这个……

关于c# - 序列不包含 ListView 的 xamarin 表单中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42717656/

相关文章:

c# 正则表达式到 javascript

c# - 确定字符串后删除字符串

c# - 在异步操作完成之前从方法返回的坏习惯?

Android PullToRefresh onScrollListener 不工作

ios - 在 Monogame 和 UIKit 之间切换

xamarin.ios - UIActivityIndi​​catorView.StartAnimating 重写 UIActivityIndi​​catorView.Hidden 绑定(bind)

ios - MvvmCross iOS : How to bind MapView Annotation to jump to another view?

c# - 如何在 Sharepoint 站点工作流程 2010 中获取当前用户?

c# - 如何获取 WPF ListView 以将字节数组格式化为逗号分隔的字符串?

java - 如何将值从 ListView 传递到 detalis Activity