【发布时间】:2017-03-24 17:02:12
【问题描述】:
我正在尝试使用数据绑定在 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 似乎是空的或不可见。
【问题讨论】:
-
你们实施INPC吗?你应该创建一个单独的 MainPageViewModel.cs...
-
你需要设置你的BindingContext:this.BindingContext = this;
-
@Jason 这不是暗示吗?
-
不,通常你会绑定到一个虚拟机,而页面无法知道你的虚拟机叫什么