ListView中的Xamarin ImageSource不更新

时间:2017-06-15 15:35:45

标签: xamarin

我有一个包含图像的ListView,图像是从服务器获取的。我制作的第一个API调用获取var people中的数据。我使用占位符图像加载listview,然后运行第二个API调用以获取listview中每个项目的图像。我收到一个byte []作为图像,然后将其转换为ImageSource。我在页面顶部有一个搜索按钮,我设置绑定到TempImage,它使用byte []作为源,并且它会更改为加载的图像。所以byte []到ImageSource的转换很好。 p.PictureImageSource = "name_circle.png"的初始设置也可以正常运行。但是,将p.PictureImageSource设置为转换后的byte []不起作用。它永远不会从最初的"name_circle.png"更改。有什么想法吗?

                        var people = peopleModel.Response;

                        if(people.Count == 0)
                        {
                            ShowNoResults = true;
                        }
                        else
                        {
                            ShowNoResults = false;
                            Results = peopleModel.Response;
                            foreach (PersonViewModel p in Results)
                            {
                                p.Initials = p.FirstName[0].ToString() + p.LastName[0];
                                p.PictureImageSource = "name_circle.png";
                            }

                        }

                        //must do 2 seperate loops so the initials load before going on with 2nd search
                        foreach (PersonViewModel p in Results)
                        {
                            IsBusy = false;
                            var peopleImage = await peopleService.GetPersonImage("p.Email");

                            if ((peopleImage.Error == null) && (peopleImage.Response != null))
                            {
                                p.Picture = peopleImage.Response;

                                byte[] imageAsBytes = (byte[])peopleImage.Response;
                                p.PictureImageSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
                                TempImage = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
                            }

                        }

                        OnPropertyChanged();

-

public class PersonViewModel : INotifyPropertyChanged
    {
        public WorkstationViewModel WorkstationDetail { get; set; }

        public List<PointViewModel> Points { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName { get; set; }
        public string Initials { get; set; }
        public string Email { get; set; }
        public string ID { get; set; }
        public string Department { get; set; }
        public string BuildingName { get; set; }
        public string SiteID { get; set; }
        public string BuildingID { get; set; }
        public string FloorNumber { get; set; }
        public string FloorID { get; set; }
        public string Workstation { get; set; }
        public string Title { get; set; }
        public string Phone { get; set; }
        public byte[] Picture { get; set; }
        public ImageSource PictureImageSource { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }

1 个答案:

答案 0 :(得分:0)

Apineda认为列表视图根本不用新数据刷新是正确的。我将两个API调用分成两种不同的方法(不确定这部分是否完全必要)。完成第二次调用后,我将listview的contrib设置为null,然后返回ItemSource值,强制刷新。现在图像显示。

Results