Miscellaneous
Binding of Image On Android
ImageView
on Android doesn't have a property to bind image by it's path. It can be easily fixed by using attached property. Just follow the next two steps:
- Let's create attached property
ImagePath
forImageView
control and register it in a module:
public class MainModule : ModuleBase
{
public MainModule() : base(false, LoadMode.All)
{
}
protected override bool LoadInternal()
{
BindingServiceProvider.MemberProvider
.Register(AttachedBindingMember
.CreateMember<ImageView, string>("ImagePath", null,
(info, view, arg) =>
{
if (string.IsNullOrEmpty(arg)) return;
// let's check it is a local resource or remote
var picId = view.Context.Resources
.GetIdentifier(arg, "drawable", view.Context.PackageName);
// load image in appropriate way
// suppose that we use Glide to show images
if (picId == 0)
Glide.With(view.Context).Load(arg).Into(view);
else
Glide.With(view.Context).Load(picId).Into(view);
}));
return true;
}
protected override void UnloadInternal()
{
}
}
- Bind image path to new attached property:
<ImageView android:id="@+id/image_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
pkg:Bind="ImagePath ImagePathProperty" />
Here we binded ImagePathProperty
property from viewmodel to new attached property ImagePath
.
Updated about 7 years ago