MvvmCross Android Bind Image from byte[]

后端 未结 2 922
时光说笑
时光说笑 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:14

    I think you could bind this using a custom UI control.

    To do this, you'll need to do something like:

    1. inherit a new MyImageView from ImageView
    2. add the default constructor (which passes the context and attributes down to the base constructor)
    3. add a new RawImage property to MyImageView, 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);
           }
      }
      

    You can then use that MyImageView control in your axml instead of the normal ImageView.

    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.


    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

提交回复
热议问题