首页 文章

图像无法在Xamarin的ListView中加载?

提问于
浏览
0

我想在我的UWP Xamarin项目中显示一个图像列表 .

我不想使用ImageCell .

这是Xamarin论坛的示例代码 .

但我无法完成此代码以成功运行 .

这是我的代码 .

<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项目的Assets文件夹中有noimage.png .

我能怎么做?

1 回答

  • 0

    我相信如果你想使用绑定,你需要 ImageItem 来实现 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"));
               }
            }
        }
    }
    

相关问题