Xamarin - 在自定义列表视图 ViewCell 中使用图像

Xamarin - Using an image in a custom list view ViewCell

我想创建一个自定义 ViewCell 以在 ListView 中使用,就像我以前做过很多次一样。然而,这是我第一次需要在自定义 ViewCell 中使用图像,但我无法让它工作。

所以我有了我的模型:

public class AttendedMeeting
{
public string Title { get; set; }
public string Date { get; set; }
public string Status { get; set; } 
public Image MeetingImage { get; set;}  
}

在我的自定义 ViewCell 中:

Label titleLabel = new Label();
titleLabel.SetBinding(Label.TextProperty, "Title");

Label dateLabel = new Label();
dateLabel.SetBinding(Label.TextProperty, "Date");

Label statusLabel = new Label();
statusLabel.SetBinding(Label.TextProperty, "Status");

Image meetingImage = new Image();
meetingImage.SetBinding(Image.SourceProperty, "MeetingImage");

然后在我的页面中,我填充了这样一个对象列表:

        ListView meetingsListView = new ListView();

        Image imageCheck = new Image();
        imageCheck.Source = "Check.png";

        Image imageLine = new Image();
        imageLine.Source = "Line.png";

        Image imageCircle = new Image();
        imageCircle.Source = "Circle.png";


        meetingsListView.ItemsSource = new AttendedMeeting[] {
                            new AttendedMeeting {Title = "Meeting 1",
                                Date="Monday - May 16th 2016",
                                Status="Verified",
                                MeetingImage=imageCheck
                                                },
                new AttendedMeeting {Title = "Meeting 2",
                                Date="Monday - May 16th 2016",
                                Status="Verified",
                                MeetingImage=imageLine
                                                },
                new AttendedMeeting {Title = "Meeting 3",
                                Date="Monday - May 16th 2016",
                                Status="Verified",
                                MeetingImage=imageCheck
                                                },
                new AttendedMeeting {Title = "Meeting 4",
                                Date="Monday - May 16th 2016",
                                Status="Verified",
                                MeetingImage=imageCheck
                                                }
        };
        meetingsListView.ItemTemplate = new DataTemplate(typeof(CellMeetingsAttended));

除了图像,一切都很棒。标签填充得很好,但每个项目上的图像始终为空白。谁能告诉我我做错了什么?我也试过在模型中使用字符串而不是 Image() 并使用字符串设置图像源,但也没有成功。我已经验证资源是正确的,我可以将它们显示为 ListView 之外的图像。

提前致谢。

你的问题是你试图直接绑定完整的 Image 对象(这是有意的吗?)当需要一个字符串时,试试这个:

new AttendedMeeting[] 

{
    new AttendedMeeting 
    {
        Title = "Meeting 1",
        Date = "Monday - May 16th 2016",
        Status = "Verified",
        MeetingImage = "Line.png"; //--Try this!
    },...

在您的模型中:

public class AttendedMeeting
{
    public string Title { get; set; }
    public string Date { get; set; }
    public string Status { get; set; }
    //Change this:
    public string MeetingImage { get; set;}  
}