2

In my app I have a button and an image. I click the button, it loads the devices photo picker and then on selection, sets the image source to the selected image.

This works fine if I set the image directly through code using (ImageName).Source = "(Source)".

Instead, what I'm doing is sending that image and storing it in Azure Storage and using the UR of that image as the binding for the images source. This works fine.

However the problem I'm encountering is when I have sent the image, I want it to update the image source without having to set it in code. I want it to be done through the view model binding.

This is because there are parts of the app that also use this URI and therefore when I update the image in the Azure Storage, I want it to then update the image in all parts of the app.

So what happens is this...

  1. Click Button and select image from photo picker.
  2. Image is sent to Azure Storage where it is stored.
  3. The image does not update.
  4. Close App.
  5. Load App.
  6. Image is now the updated image.

I'm essentially looking for a way to "refresh" the URI. I'm sure it has something to do with the way the caching of images works, but I'm not entirely sure. The image is loaded if I close the app and open it again but this is not an ideal solution.

To summarise: I need a way to update/refresh the URI of the image that its source is bound to in the view model so that it loads the newly added image.

Here is my xaml...

 <ffimageloading:CachedImage HeightRequest="125"
                             WidthRequest="125" 
                             x:Name="img_dp"
                             LoadingPlaceholder="ic_user_default.png"
                             ErrorPlaceholder="ic_user_default.png"
                             CacheDuration="0"
                             CacheType="None"
                             Source="{Binding DisplayPicture}">
                        </ffimageloading:CachedImage>

Here is my view model binding...(UserInfo.DisplayPicture is the URI)

private string _displayPicture { get; set; } = UserData.UserInfo.DisplayPicture;
    public string DisplayPicture
    {
        get
        {
            if (_displayPicture == null)
            {
                return "ic_user_default";
            }
            else
            {


                return _displayPicture;
            }
        }
        set
        {
            _displayPicture = value;
            OnPropertyChanged(nameof(DisplayPicture));
        }
    }

PS: I have tried

  • With and without OnPropertyChanged.
  • Using ImageSource type instead of string for the binding type.
  • Using FFImageLoadings features to clear the cache.
Lewis
  • 55
  • 1
  • 7
  • Not exactly sure how things work in Xamarin. But with an URL string, the actual property value won't change (it will always be the same URL), so the PropertyChanged is simply ignored. With ImageSource as property type it should work if you create a new ImageSource instance and disable all image caching. – Clemens Dec 15 '18 at 21:26
  • Me too facing similar problem anyone found any solution? Clearing cache does solve problem but that's not feasible .. – niketan Apr 15 '19 at 21:36

0 Answers0