MvvmCross Android Bind Image from byte[]

后端 未结 2 896
时光说笑
时光说笑 2021-01-23 00:46

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 wa

2条回答
  •  故里飘歌
    2021-01-23 01:20

    You can do it even easier and I also found that out after stumbling on that question:

    As the Android-binding for setting a Bitmap for a ImageView is Bitmap (as you can see at [1]), you can configure your view like this:

        
    

    Then you only need to define a ValueConverter, called ByteArrayToImageValueConverter, that converts the byte array to an instance of Bitmap. For me, using a ValueConverter is the preferred way over creating custom binding ;)

    You already had the code for converting a byte array to a bitmap: BitmapFactory.DecodeByteArray(_rawImage, 0,_rawImage.Length);

    [1] https://github.com/MvvmCross/MvvmCross/blob/bbf9a2ac76e74d9404f4b57036c6e29dfe2cc6c3/Cirrious/Cirrious.MvvmCross.Binding.Droid/MvxAndroidBindingBuilder.cs#L79

提交回复
热议问题