微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin绑定不适用于生成的可观察集合

如何解决Xamarin绑定不适用于生成的可观察集合

我的问题归结为为什么我的可观察集合绑定功能不起作用?我正在尝试将图像URL绑定到ListView,但是绑定将不起作用,几乎只能制作产品图像列表。如果仅执行堆栈布局并根据列表位置声明图像(即image.source = list [i] .url),就可以生成图像。

下面是xaml

    <ListView ItemsSource="{Binding Actimage}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Image Source="{Binding ImageUrl}"/>
                </StackLayout>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

下面是xaml后面的c,其中“ PicListPull”是模型,图片是先前生成的图像URL列表。

    private ObservableCollection<PicListPull> actimage;
    public ObservableCollection<PicListPull> Actimage;
    Actimage = new ObservableCollection<PicListPull>();
        for (int i = 0; i < pictures.Count(); i++)
        {
            var pics = new PicListPull()
            {
                ImageUrl = ImageSource.FromUri(pictures[i].Url)
            };
            Actimage.Add(pics);

        }

如果我在索引处查看Actimage,它具有图像源的值,因此我无法弄清楚为什么它不显示图像。

谢谢。

更新

下面是PicListPull

    public class PicListPull:INotifyPropertyChanged
{
    private ImageSource imageurl;
    //#region public properties
    public ImageSource ImageUrl
    {
        get { return imageurl; }
        set
        {
            this.imageurl = value;
            RaisePropertyChanged("ImageUrl");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(String name)
    {
        if (PropertyChanged != null)
            this.PropertyChanged(this,new PropertyChangedEventArgs(name));
    }
}

解决方法

ListView的单元格应为以下类型之一: TextCell,ImageCell,SwitchCell,EntryCell或CustomCell(ViewCell)

在这种情况下,如果您要使用自定义单元格,请进行如下更新:

<ListView ItemsSource="{Binding ActImage}">
        <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                <StackLayout>
                    <Image Source="{Binding ImageUrl}"/>
                </StackLayout>
              </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

有关更多详细信息,您可以在此处查看:Custom Cells

,

您需要解决三个问题

首先,您只能绑定到公共属性

ItemsSource="{Binding ActImage}"

要使其正常工作,ActImage必须是公共财产

public ObservableCollection<PicListPull> ActImage { get; set; }

第二秒,您需要在模板中使用Cell(如@Qwerty所述)

    <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <StackLayout>
                <Image Source="{Binding ImageUrl}"/>
            </StackLayout>
          </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>

最后,在绑定Image时,您可以只使用包含url的字符串

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。