2017-03-24 29 views
1

データバインディングを使用してxamarinフォームで単純なリストアプリケーションを作成しようとしていますが、データが表示されません。 は、ここに私のMainPage.xamlをXamarin 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" 
      xmlns:local="clr-namespace:Practice3" 
      x:Class="Practice3.MainPage"> 
    <StackLayout> 
     <ListView ItemsSource="{Binding Films}"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
        <ImageCell 
        Text="{Binding Title}" 
        TextColor="Black" 
        Detail="{Binding Resume}" 
        DetailColor="Gray" 
       /> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
    </StackLayout> 
</ContentPage> 

は、ここで私はちょうどStackLayoutにラベルを入れてみました、それは登場MainPage.xaml.cs

namespace Practice3 
{ 
    public partial class MainPage : ContentPage 
    { 
     public ObservableCollection<Film> Films { get; set; } 

     public MainPage() 
     { 
      InitializeComponent(); 

      Films = new ObservableCollection<Film>{ 
       new Film{ 
        Title = "X-Men (2000)", 
        Resume = "Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers." 
       }, 
       new Film{ 
        Title = "X-Men 2 (2003)", 
        Resume = "The X-Men band together to find a mutant assassin who has made an attempt on the President's life, while the Mutant Academy is attacked by military forces." 
       }, 
       new Film{ 
        Title = "X-Men: The last stand (2006)", 
        Resume = "When a cure is found to treat mutations, lines are drawn amongst the X-Men, led by Professor Charles Xavier, and the Brotherhood, a band of powerful mutants organized under Xavier's former ally, Magneto." 
       } 
      }; 
     } 
    } 
} 

そしてFilms.cs

namespace Practice3 
{ 
    public class Film 
    { 
     public string Title { get; set; } 
     public string Resume { get; set; } 
    } 
} 

だです明らかに、ListViewは空または不可視であるようです。

+0

あなたはINPCを実装するのですか?別のMainPageViewModel.csを作成する必要があります。 –

+1

BindingContextを設定する必要があります:this.BindingContext = this; – Jason

+0

@Jasonそれは暗黙のうちではない? –

答えて

0

私は最終的に、このようにそれを解決: MainPage.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" 
      xmlns:local="clr-namespace:Practice3" 
      x:Class="Practice3.MainPage"> 
    <StackLayout> 
     <ListView x:Name="FilmList"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
        <ImageCell 
        Text="{Binding Title}" 
        TextColor="Black" 
        Detail="{Binding Resume}" 
        DetailColor="Gray" 
       /> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
    </StackLayout> 
</ContentPage> 

そしてMainPage.xaml.cs

namespace Practice3 
{ 
    public partial class MainPage : ContentPage 
    { 
     public ObservableCollection<Film> Films { get; set; } 

     public MainPage() 
     { 
      InitializeComponent(); 

      Films = new ObservableCollection<Film>{ 
       new Film{ 
        Title = "X-Men (2000)", 
        Resume = "Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers." 
       }, 
       new Film{ 
        Title = "X-Men 2 (2003)", 
        Resume = "The X-Men band together to find a mutant assassin who has made an attempt on the President's life, while the Mutant Academy is attacked by military forces." 
       }, 
       new Film{ 
        Title = "X-Men: The last stand (2006)", 
        Resume = "When a cure is found to treat mutations, lines are drawn amongst the X-Men, led by Professor Charles Xavier, and the Brotherhood, a band of powerful mutants organized under Xavier's former ally, Magneto." 
       } 
      }; 
      FilmList.ItemsSource = Films; 
     } 
    } 
} 
+0

が動作しますが、上記のようにページのBindingContextを割り当てるだけで解決できます。 – Jason

+0

True、私はそれが解決されるまであなたの答えを見なかった。ありがとうございました。 – JOSEMAFUEN

関連する問題