在cs代碼中完成
自定義單元格
public class EmployeeCell : ViewCell
{
public EmployeeCell()
{
var image = new Image
{
HorizontalOptions = LayoutOptions.Start
};
image.SetBinding(Image.SourceProperty, new Binding("ImageUri"));
//設(shè)置寬高為40
image.WidthRequest = image.HeightRequest = 40;
var nameLayout = CreateNameLayout();
var viewLayout = new StackLayout()
{
Orientation = StackOrientation.Horizontal,
//加入圖片、名稱、推特
Children = { image, nameLayout }
};
//把布局賦值給View
View = viewLayout;
}
static StackLayout CreateNameLayout()
{
//新增Label
var nameLabel = new Label
{
HorizontalOptions = LayoutOptions.FillAndExpand
};
//綁定Employee的DisplayName屬性
nameLabel.SetBinding(Label.TextProperty, "DisplayName");
var twitterLabel = new Label
{
HorizontalOptions = LayoutOptions.FillAndExpand
};
twitterLabel.SetBinding(Label.TextProperty, "Twitter");
var nameLayout = new StackLayout()
{
HorizontalOptions = LayoutOptions.StartAndExpand,
//設(shè)置為從上到下排列
Orientation = StackOrientation.Vertical,
//將兩個(gè)Label依次添加到Children中
Children = { nameLabel, twitterLabel }
};
return nameLayout;
}
}
后臺(tái)
List<Employee> EmployeeList = new List<Employee>();
EmployeeList.Add(new Employee()
{
DisplayName = "Jack",
Twitter ="@fake4",
ImageUri= "http://v1.qzone.cc/avatar/201406/24/21/03/53a977066f053731.jpg!200x200.jpg"
});
EmployeeList.Add(new Employee()
{
DisplayName = "Tom",
Twitter = "@mml1",
ImageUri= "http://diy.qqjay.com/u2/2014/0628/da687c0fb5b3bb8cd31dec7d8865aea8.jpg"
});
EmployeeList.Add(new Employee()
{
DisplayName = "Tony",
Twitter = "@wood564",
ImageUri= "http://v1.qzone.cc/avatar/201406/24/21/03/53a977066f053731.jpg!200x200.jpg"
});
var listView = new ListView
{
RowHeight = 80
};
listView.ItemsSource = EmployeeList;
//注意:此時(shí)指定模板為寫好的EmployeeCell
listView.ItemTemplate = new DataTemplate(typeof(EmployeeCell));
Content = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Children = { listView }
};
Employee類
public class Employee
{
public string DisplayName { get; set; }
public string Twitter { get; set; }
public string ImageUri { get; set; }
}
效果

1
在xaml中完成
后臺(tái)賦值
List<Employee> Employees = new List<Employee>();
//添加數(shù)據(jù)
BindingContext = Employees;
前端綁定對(duì)應(yīng)屬性名
BindingContext已經(jīng)賦值,ItemsSource直接綁定下來(lái)即可
<ListView x:Name="listView" ItemsSource="{Binding }">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal">
<Image Source="{Binding ImageUri}" WidthRequest="40" HeightRequest="40" />
<StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand">
<Label Text="{Binding DisplayName}" HorizontalOptions="FillAndExpand" />
<Label Text="{Binding Twitter}"/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
注意:Employee實(shí)不實(shí)現(xiàn)INotifyPropertyChanged接口,均可以展示,只是看你做不做雙向綁定