android mvvm 找不到属性绑定,MVVMCross将ImageView交叉绑定到android资源ID而不是资产路径...

博客讲述了如何在Android应用中利用MvvmCross框架,通过一个ValueConverter来根据业务逻辑动态地从资源文件或Drawable中设置ImageView的图片。作者目前的方法是从Assets加载图片,并通过一个转换器根据枚举值决定图片路径。作者希望改为直接使用资源 Drawable,但尚未找到合适的方式。讨论了可能的解决方案,包括修改转换器返回资源ID、创建自定义MvxImageView或实现自定义绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have a layout with an ImageView in it. I want to show a different image, depending on logic in my core project. Adding the images to the assets folder and binding it up with a converter works. For my ValueConverter:

public class MyImageValueConverter : MvxValueConverter

{

protected override string Convert(MyMap value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

return GetResourcePath(value);

}

private string GetResourcePath(MyMap map)

{

string path = "";

switch (map)

{

case MyMap.Map1:

path = "map1.png";

break;

case MyMap.Map2:

path = "map2.png";

break;

default:

throw new Exception("no image for map: " + map.ToString());

}

return path;

}

}

In my view, i bind it up like this:

...

var map = FindViewById(Resource.Id.my_imageview);

set.Bind(map).For("AssetImagePath").To(vm => vm.CurrentMap).WithConversion("MyImage");

...

Now to my question. I would very much prefer to set the image from a resource drawable instead, but i have not found a way to do this. I feel like i should just be able to return a resource ID from my converter, and bind it to some property (which?).

Alternatively i could just create a new class that inherits from MvxImageView and add a property that i could do a one-way binding to, that calls SetImageResource(). Or even go the full custom binding way. None of these seem very nice.

Solutions1

Bind to MvxImageView's DrawableId

Look to MvxAndroidBindingBuilder, it has some hidden gems:

Talk1:

Thank you! The BindingBuilder is an excellent resource to discover these otherwise somewhat "hidden" binding properties

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值