0

I am still kind of new to Xamarin forms and now I am using the Ffimagloading library to display a gif through my viewmodel's "DisplayImage" property. But after certain conditions are met, I want to hide/unload the image, so that it is no longer there. This is the CachedImage in my View:

                <ffimage:CachedImage Grid.Column="0"
                                   Source="{Binding DisplayImage}" />

And this is the corresponding part in my ViewModel:

    private ImageSource displayImage;

    public void DownloadAndAssignImage() {
        try
        {
            var response = await this.DownloadFile(new Uri("..."));
            if (response != null)
            {
                this.DisplayImage = ImageSource.FromStream(() => new MemoryStream(response));
                
            }
        }
        catch (Exception e)
        {
            Log.Error(e);
        }
    }


    public void HideImage()
    {
        // does not work
        this.DisplayImage = null;
        
        // does not work too
        this.DisplayImage = new byte[0];
    }

    public ImageSource DisplayImage
    {
        get => this.displayImage;
        set
        {
            this.displayImage= value;
            this.RaisePropertyChanged();
        }
    }

How can I make it, so that the CachedImage shows nothing again after having assigned an ImageSource to it through "DownloadAndAssignImage()"? Setting the ImageSource to null or to an empty byte[] array does not work. How exactly do I need to modify the "HideImage()" method?

Thanks for your help in advance!

Ro0f3r
  • 5
  • 1

1 Answers1

0

use IsVisible

<ffimage:CachedImage Grid.Column="0" IsVisible="{Binding ImageVisible}"
                               Source="{Binding DisplayImage}" />

then in your VM (you'll need to implement INotifyPropertyChanged)

ImageVisible = false;
Jason
  • 86,222
  • 15
  • 131
  • 146
  • Thank you for your answer. Unfortunately I shortened the code a little bit. There is also a background in the CachedImage, which I want to have displayed independent of the Image Source. The background would also disappear if I set the visibility to False. That's why I'm looking for a way to reset the Source property. – Ro0f3r Apr 08 '21 at 11:04
  • using a placeholder image would probably be the easiest approach – Jason Apr 08 '21 at 11:06
  • Is there a convenient way to do that? – Ro0f3r Apr 08 '21 at 14:41
  • @Ro0f3r When I add background and effective image source for CachedImage, it just display Image source, don't display background. If I set CachedImage source=null, background also can not display, so as jason's opinion, you can use placeholder image:https://forums.xamarin.com/discussion/94350/display-placeholder-image-when-image-does-not-load-of-uriimagesource – Cherry Bu - MSFT Apr 09 '21 at 02:26