我正在尝试使用数据绑定在xamarin表单中创建一个简单的列表应用程序,但数据不会出现。 这是我的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 ItemsSource="{Binding Films}">
<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."
}
};
}
}
}
和Films.cs
namespace Practice3
{
public class Film
{
public string Title { get; set; }
public string Resume { get; set; }
}
}
我试图在StackLayout中放置一个Label,它看起来很清楚,但ListView似乎是空的或不可见的。
答案 0 :(得分:1)
我终于解决了这个问题: 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;
}
}
}