2016-12-08 2 views
0

UWP Xamarin 프로젝트에 이미지 목록을 표시하려고합니다.Xamarin의 ListView에서 이미지를로드 할 수 없습니까?

ImageCell을 사용하고 싶지 않습니다.

이것은 Xamarin Forum의 샘플 코드입니다.

하지만 성공적으로 실행하려면이 코드를 완료 할 수 없습니다.

여기 내 코드입니다.

<ListView x:Name="listView"> 
     <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell> 
      <StackLayout BackgroundColor="#eee" 
      Orientation="Vertical"> 
       <StackLayout Orientation="Horizontal"> 
       <Image Source="{Binding image}" /> 
       <Label Text="{Binding title}" 
       TextColor="#f35e20" /> 
       <Label Text="{Binding subtitle}" 
       HorizontalOptions="EndAndExpand" 
       TextColor="#503026" /> 
       </StackLayout> 
      </StackLayout> 
      </ViewCell> 
     </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

public class ImageItem { 
string title; 
ImageSource image; 
string subtitle; 
} 
ImageItem a= new ImageItem(); 
a.title = "XXX"; 
a.image = ImageSource.FromFile(String.Format("{0}{1}.png", Device.OnPlatform("Icons/", "", "Assets/"), "noimage")); 
a.subtitle = "XXX"; 
list.Add(a); 
listview.itemsSource = list; 

UWP Xamarin Project의 자산 폴더에 noimage.png이 있습니다.

어떻게하면됩니까?

답변

0

바인딩을 사용하고 싶다면 인터페이스를 구현하는 데 도움이됩니다. 다음과 같이 ImageItem 클래스를 설정할 수 있습니까?

public class ImageItem : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    string _title; 
    public string title 
    { 
     get 
     { 
      return _title; 
     } 
     set 
     { 
      if (_title != value) 
       _title = value; 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("title")); 
      } 
     } 
    } 

    ImageSource _image; 
    public string image 
    { 
     get 
     { 
      return _image; 
     } 
     set 
     { 
      if (_image != value) 
       _image = value; 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("image")); 
      } 
     } 
    } 

    string _subtitle; 
    public string subtitle 
    { 
     get 
     { 
      return _subtitle; 
     } 
     set 
     { 
      if (_subtitle != value) 
       _subtitle = value; 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("subtitle")); 
      } 
     } 
    } 
} 
+0

여전히 동일합니다. 이미지뿐만 아니라 제목과 부제목도 표시합니다. –

+0

그러면 ImageSource.FromFile (String.Format ("{0} {1} .png", Device.OnPlatform ("아이콘 /", "자산"/ "이미지 없음"));'. 이미지가 플랫폼 프로젝트에 있어야하는 위치라면 파일 이름 만 전달하면됩니다. 따라서'ImageSource.FromFile ("noimage.png"));'를 사용해보십시오. – jgoldberger

관련 문제