【发布时间】:2018-04-13 14:28:26
【问题描述】:
在 Xamarin.Android 中,要使用 FFImageLoading 加载图像,必须使用 ImageViewAsync 而不是标准 ImageView。
如果我想将图像加载到 ImageButton 中,我找不到可以使用的内容。没有找到“ImageButtonAsync”。
【问题讨论】:
标签: xamarin xamarin.android ffimageloading
在 Xamarin.Android 中,要使用 FFImageLoading 加载图像,必须使用 ImageViewAsync 而不是标准 ImageView。
如果我想将图像加载到 ImageButton 中,我找不到可以使用的内容。没有找到“ImageButtonAsync”。
【问题讨论】:
标签: xamarin xamarin.android ffimageloading
不知道这是否相关。我给了我的 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 是您创建的方法。在那里你将把你真正想要点击手势做什么的逻辑放在里面。
您也可以使用命令传递对象。让我们知道这是否有意义
【讨论】:
AFAIK 没有特定的 ImageButton 与 FFImageLoading 一起使用,但您可以直接使用 ImageViewAsync 将其设置为 android:clickable="true" 并添加点击事件。
如前所述,here 和 ImageButton 只是一个ImageView,默认情况下具有非空背景,并且 ImageButton.onSetAlpha() 方法总是返回 false,scaleType 设置为 center 并且总是膨胀作为可聚焦的。因此,如果您需要该行为,您可以添加它。
另一种方法是创建可以处理 FFImageLoading 图像加载的自定义 ImageButton。为此,您可以从ImageViewAsync 继承并添加前面段落中解释的行为。这样你就可以直接使用FFImageLoading API,因为这个自定义控件继承了ImageViewAsync。
除此之外,您还可以按照 here 的说明添加自定义加载逻辑,继承自 ImageLoaderTask,并将其称为 ImageService.Instance.LoadImage(customImageTask)
最后,另一种方法(但 hackish 且性能不佳)是创建一个 ImageViewAsync 来保存 FFImageLoading 的结果,并在 Success 回调中将 Drawable 设置为 ImageButton:
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);
嗨,如果您需要任何帮助,请告诉我
【讨论】: