MvvmCross Android Bind Image from byte[] [英] MvvmCross Android Bind Image from byte[]
问题描述
有谁知道如何在 axml 视图中将 byte[](图像)绑定到 Image 控件.我的 ViewModel 继承自 MvxViewModel.我所有其他绑定都很好用,但我找不到绑定该图像的方法.
Does anyone know how to bind a byte[] (image) to a Image control in a axml view. My ViewModel inherit from MvxViewModel. All my other bindings works great but I cannot find a way to bind that image.
推荐答案
我认为您可以使用自定义 UI 控件绑定它.
I think you could bind this using a custom UI control.
为此,您需要执行以下操作:
To do this, you'll need to do something like:
- 从
ImageView
继承一个新的 - 添加默认构造函数(将上下文和属性向下传递给基本构造函数)
向
MyImageView
添加一个新的RawImage
属性,实现为:
MyImageView
- inherit a new
MyImageView
fromImageView
- add the default constructor (which passes the context and attributes down to the base constructor)
add a new
RawImage
property toMyImageView
, implementing it as:
private byte[] _rawImage;
public byte[] RawImage
{
get { return _rawImage; }
set
{
_rawImage = value;
if (_rawImage == null)
return;
var bitmap = BitmapFactory.DecodeByteArray(_rawImage, 0,_rawImage.Length);
SetImageBitmap(bitmap);
}
}
然后您可以在 axml 中使用 MyImageView
控件而不是普通的 ImageView
.
You can then use that MyImageView
control in your axml instead of the normal ImageView
.
注意——上面的这段代码没有经过测试——但是一旦你在视图中得到了字节[],我相信你会弄清楚要使用什么 Droid 代码.
Note - this code above not tested - but once you get the byte[] in the View I'm sure you'll work out what Droid code to use.
作为替代方法,您还可以使用自定义绑定将 byte[]
绑定到普通的 ImageView
- 请参阅 byte[]
中的自定义绑定示例a href="https://stackoverflow.com/questions/10700445/in-mvvmcross-how-do-i-do-custom-bind-properties/">在 MvvmCross 中如何自定义绑定属性
As an alternative approach to this, you could also use a custom binding to bind a byte[]
to a normal ImageView
- see an example of custom binding in In MvvmCross how do I do custom bind properties
这篇关于MvvmCross Android Bind Image from byte[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!