2016-12-08 13 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のAssetsフォルダにnoimage.pngがあります。

どうすればいいですか?

答えて

0

バインディングを使用する場合はを実装してINotifyPropertyChangedインターフェイスを実装する必要があります。あなたは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(" Icons/"、" "Assets /")、 "noimage") ); '。画像がプラットフォームプロジェクトのどこにあるか分からない限り、ファイル名を渡すだけでよいので、 'ImageSource.FromFile(" noimage.png "));'を試してみてください。 – jgoldberger

関連する問題