如何以 xamarin 形式隐藏/卸载 CachedImage

问题描述

我还是 Xamarin 表单的新手,现在我使用 Ffimagloading 库通过我的视图模型的“displayImage”属性显示 gif。但是在满足某些条件后,我想隐藏/卸载图像,使其不再存在。 这是我视图中的 CachedImage:

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

这是我的 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();
        }
    }

我该如何实现,以便在通过“DownloadAndAssignImage()”为其分配 ImageSource 后,CachedImage 不再显示任何内容?将 ImageSource 设置为 null 或空字节 [] 数组不起作用。我究竟需要如何修改“HideImage()”方法

提前感谢您的帮助!

解决方法

使用IsVisible

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

然后在您的 VM 中(您需要实现 INotifyPropertyChanged)

ImageVisible = false;