2017-03-10 33 views
6

Tôi đang cố gắng đưa một ListView từ một dữ liệu. Trong file XAML của tôi, tôi đã viếtkhông chứa các phần tử trong các biểu mẫu xamarin cho listview

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

Trong tập tin 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; 
     } 
    } 
} 

Mỗi lần tôi xây dựng dự án này tôi nhận được lỗi "Trình tự không chứa yếu tố". Tôi đã thấy một số hướng dẫn trực tuyến có cùng mã và chạy tốt. Có gì sai ở đây?

Trả lời

18

Bạn phải bao gồm điều khiển bên trong một bố cục

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

Tôi đã viết little article này về vấn đề này ...

0

Đôi khi điều này xảy ra khi bạn có một ListView với Bindings và khi bạn quên thêm Ràng buộc từ khóa

Ví dụ: {Binding Name}

Các vấn đề liên quan