2016-09-21 101 views
0

我正在創建一個自定義的ViewCell,以便在ListView中使用,就像我以前做過很多次一樣。但是,這是我第一次需要在自定義ViewCell中使用圖像,並且無法使其工作。Xamarin - 在自定義列表視圖中使用圖像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外部的圖像。

在此先感謝。

回答

2

你的問題是,你正試圖將完整的圖像對象直接綁定(是故意的嗎?)當一個字符串預計,試試這個在代替:

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;} 
} 
+1

非常感謝。這工作加上它也導致我另一個領域,我沒有正確設置來源,工程太棒了! – AS2012