【发布时间】:2015-02-25 13:20:11
【问题描述】:
创建我的第一个根页面:
public class App : Application
{
public App ()
{
MainPage = new NavigationPage (new ListExample ());
}
}
这是我的员工对象:
public class Employee
{
public string ImageUri { get; set; }
public string DisplayName { get; set; }
public string Twitter { get; set; }
public Employee (string imageUri, string displayName, string twitter)
{
this.ImageUri = imageUri;
this.DisplayName = displayName;
this.Twitter = twitter;
}
}
这就是我为UITableView 设置项目的方式:
public class ListExample : ContentPage
{
public ListExample ()
{
var listView = new ListView
{
RowHeight = 40
};
List<Employee> myListOfEmployeeObjects = new List<Employee> {
new Employee("", "Max Mustermann", "@fake1"),
new Employee("", "Eva Mustermann", "@fake2")
};
listView.ItemsSource = myListOfEmployeeObjects;
listView.ItemTemplate = new DataTemplate(typeof(EmployeeCell));
}
}
这是我的自定义表格视图单元格:
public class EmployeeCell : ViewCell
{
public EmployeeCell()
{
var image = new Image
{
HorizontalOptions = LayoutOptions.Start
};
image.SetBinding(Image.SourceProperty, new Binding("ImageUri"));
image.WidthRequest = image.HeightRequest = 40;
var nameLayout = CreateNameLayout();
var viewLayout = new StackLayout()
{
Orientation = StackOrientation.Horizontal,
Children = { image, nameLayout }
};
View = viewLayout;
}
static StackLayout CreateNameLayout()
{
var nameLabel = new Label
{
HorizontalOptions= LayoutOptions.FillAndExpand
};
nameLabel.SetBinding(Label.TextProperty, "DisplayName");
var twitterLabel = new Label
{
HorizontalOptions = LayoutOptions.FillAndExpand
};
twitterLabel.SetBinding(Label.TextProperty, "Twitter");
var nameLayout = new StackLayout()
{
HorizontalOptions = LayoutOptions.StartAndExpand,
Orientation = StackOrientation.Vertical,
Children = { nameLabel, twitterLabel }
};
return nameLayout;
}
}
如果我理解正确,必须在自定义表格视图单元格中设置绑定。系统获取我的对象的属性,并且属性的名称和绑定的名称必须匹配。
使用上面的代码会导致一个空视图。
我做错了什么?
【问题讨论】:
-
找不到任何错误,
ListView在这里运行良好。可能你的ListView放错了?你能在这里发布Page布局吗? -
@AlexLau:感谢您的回复。我编辑了我的问题并包含了更多代码。表格视图使用默认布局。现在我切换到自定义单元格,但出了点问题。
标签: c# ios xamarin.ios xamarin.forms