FFImageLoading :加载到 ImageButton

FFImageLoading : load into an ImageButton

在 Xamarin.Android 中,要使用 FFImageLoading 加载图像,必须使用 ImageViewAsync 而不是标准 ImageView。

如果我想将我的图像加载到 ImageButton 中,我找不到我可以使用的东西。未找到 "ImageButtonAsync"。

AFAIK 没有特定的 ImageButton 与 FFImageLoading 一起使用,但您可以直接使用 ImageViewAsync 将其设置为 android:clickable="true" 并添加点击事件。

并且如前所述 here ImageButton 只是一个 ImageView 默认情况下具有非空背景,而且 ImageButton.onSetAlpha() 方法总是 returns false,scaleType 设置为居中,并且始终膨胀为可聚焦。因此,如果您需要该行为,您可以添加它。

另一种方法是创建可以处理 FFImageLoading 图像加载的自定义 ImageButton。为此,您可以继承 ImageViewAsync 并添加前一段中解释的行为。这样您就可以直接使用 FFImageLoading API 因为这个自定义控件继承了 ImageViewAsync.

除此之外,您还可以按照 here 继承自 ImageLoaderTask 的说明添加自定义加载逻辑,并将其命名为 ImageService.Instance.LoadImage(customImageTask)

最后,另一种方法(但很老套且性能不佳)是创建一个 ImageViewAsync 来保存 FFImageLoading 的结果,并在 Success 回调中设置 ImageButton 中的 Drawable:

var myImageButton = myView.FindViewById<ImageButton>(Resource.Id.my_image_button);
var myImageView = new ImageViewAsync(this); // this is the context
ImageService.Instance.LoadUrl(this.Source[position].LogoImagePath)
                .Success(() => myImageButton.SetImageDrawable(myImageView.Drawable))
                .Into(myImageView);

嗨,如果您需要任何帮助,请告诉我

不知道这是否相关。我给了我的 svg 一个点击手势,让它像按钮一样工作。

首先在您的 xaml

中引用 FFImageLoading.Svg.Forms
namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms"

其次添加带有点击手势的 svgImage

    <ffimageloadingsvg:SvgCachedImage Source="YourImageSource" >
                    <ffimageloadingsvg:SvgCachedImage.GestureRecognizers>
                        <TapGestureRecognizer NumberOfTapsRequired="1" Command="{Binding YourCommand}"/>
                    </ffimageloadingsvg:SvgCachedImage.GestureRecognizers>
    </ffimageloadingsvg:SvgCachedImage>

您将在 ViewModel 的构造函数中调用它来初始化命令

YourCommand= new DelegateCommand(async () => await ExecuteYourCommandAsync());

创建绑定到 xaml 文件的属性

    public ICommand YourCommand
    {
        get => yourCommand;
        set => SetProperty(ref yourCommand, value);
    }

    private ICommand yourCommand;

await ExecuteYourCommandAsync 是您创建的方法。并在其中放置您实际希望点击手势执行的操作的逻辑。

您也可以通过命令传递对象。让我知道这是否有意义