【问题标题】:Xamarin ImageSource in ListView not updatingListView 中的 Xamarin ImageSource 未更新
【发布时间】:2017-06-15 15:35:45
【问题描述】:

我有一个包含图像的 ListView,图像是从服务器获取的。我进行的第一个 API 调用获取了var people 中的数据。我使用占位符图像加载列表视图,然后运行第二个 API 调用以获取列表视图中每个项目的图像。我收到一个 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;
    }

【问题讨论】:

  • 你的PersonViewModel是什么样的?
  • 这是一个具有public string Emailpublic byte[] Picturepublic ImageSource PictureImageSource属性的公共类。
  • PersonViewModel 是否实现了INotifyPropertyChanged
  • @WillNasby 实际上我想查看代码来解决@mark 刚刚提出的问题。您的 PersonViewModel 需要实现 INotifyPropertyChanged 以便其属性之一的更新触发通知。
  • 对不起,我添加了完整的代码。我添加了INotifyPropertyChanged,但问题仍然存在。其他一切都在更新(姓名、缩写等)。

标签: xamarin


【解决方案1】:

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

searchGestureRecognizer.Tapped += async (s, e) => {
                await _viewModel.GetResults();
                await _viewModel.GetImagesForResults();
                resultsListView.ItemsSource = null;
                resultsListView.ItemsSource = _viewModel.Results;
            };

【讨论】:

    猜你喜欢
    • 2016-10-03
    • 2018-10-16
    • 1970-01-01
    • 2020-06-12
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多