Binding shape in an xml file to an MvxImageView on Android in MvvmCross

533 views Asked by At

I've create a shape in an xml file. And now I'm trying to bind this shape to an MvxImageView. I've added a converter because I want a different color when it's in a selected state. But when I run my app, no images are shown and I'm getting the following in my application output:

[skia] --- SkImageDecoder::Factory returned null
[skia] --- SkImageDecoder::Factory returned null
[skia] --- SkImageDecoder::Factory returned null

This is the converter I wrote to set another image when in selected state:

public class BoolCircleImageValueConverter : MvxValueConverter<bool, string>
{
    protected override string Convert(bool value, Type targetType, object parameter, CultureInfo culture)
    {
        return value ? "res:yellow_circle" : "res:light_grey_circle";
    }
}

The MvxImageView with the converter:

<MvxImageView
    android:id="@+id/circle"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    local:MvxBind="ImageUrl Selected,Converter=BoolCircleImage;" />

And this is the shape I've created:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<solid android:color="@color/yellow" />
</shape>
1

There are 1 answers

1
Stuart On BEST ANSWER

I don't your approach will work - the res: scheme in MvvmCross is used to work with bitmaps - see the source in https://github.com/MvvmCross/MvvmCross/blob/900b6dd6a837e97c7204f832c4c6344d05bcf582/Plugins/Cirrious/DownloadCache/Cirrious.MvvmCross.Plugins.DownloadCache.Droid/MvxAndroidLocalFileImageLoader.cs#L35

For your particular sample it might be easier to use a custom property or a custom binding - e.g. inherit from Android's ImageView and add a property:

   private string _image;
   public string Image
   {
      get { return _image; }
      set { 
          _image = value;
          // you might have to do some tweaking on this code below to get it to work...
          var id = Resources.GetIdentifier(value, "drawable", PackageName);
          SetImageResource(id);
      }
   }