我刚开始使用 Xamarin.Forms,我有一个关于 ListView 的问题,但在网上找不到答案。在下面的代码中,我需要在 ItemTemplate 中获取元素的索引或访问单元格中显示的 ItemSource 的方法,因为我需要构建显示图像的路径。如果我可以同时拥有索引和元素,那就更好了。任何不使用 CustomRenderer 的方法?
ListView listView = new ListView
{
HasUnevenRows = true,
// Source of data items.
ItemsSource = DataManager.GetPeople(),
ItemTemplate = new DataTemplate(() =>
{
// Create views with bindings for displaying each property.
Label nameLabel = new Label();
nameLabel.SetBinding(Label.TextProperty, "Name");
nameLabel.TextColor = Color.White;
var relativeLayout = new RelativeLayout {};
var webImage = new Image { Aspect = Aspect.AspectFill };
image.HeightRequest = 200;
image.Source = ImageSource.FromFile(Path.Combine(path, "/people/263/1.jpg"));
relativeLayout.Children.Add(image,Constraint.Constant(0),Constraint.Constant(0));
relativeLayout.Children.Add(nameLabel,
Constraint.RelativeToParent((parent) => { …Run Code Online (Sandbox Code Playgroud)