从字节MvvmCross的Android绑定图像[] [英] MvvmCross Android Bind Image from byte[]
问题描述
有谁知道如何一个byte [](图片)绑定到一个AXML视图中的图像控制。 我的视图模型继承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:
- 在继承一个新的
MyImageView
从的ImageView
- 添加默认的构造方法(通过上下文和属性到基构造)
-
新
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);
}
}
然后,您可以使用 MyImageView
在AXML而不是正常的 ImageView的控制
。
You can then use that MyImageView
control in your axml instead of the normal ImageView
.
请注意 - 这code以上没有测试 - 但一旦你得到的byte []中查看我敢肯定,你会工作了使用什么样的Droid的code
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.
作为一种替代方法,您也可以使用自定义绑定绑定字节[]
来正常的ImageView
- 见<绑定定制的实例href="http://stackoverflow.com/questions/10700445/in-mvvmcross-how-do-i-do-custom-bind-properties/">In 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绑定图像[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!