将 xml 文件中的形状绑定到 MvvmCross 中 Android 上的 MvxImageView

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

我在 xml 文件中创建了一个形状。现在我正在尝试将此形状绑定到 MvxImageView。我添加了一个转换器,因为当它处于选定状态时我想要一种不同的颜色。但是当我 运行 我的应用程序时,没有显示任何图像,我在我的应用程序输出中得到以下内容:

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

这是我写的转换器,用于在处于选中状态时设置另一个图像:

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";
    }
}

带有转换器的 MvxImageView:

<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;" />

这是我创建的形状:

<?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>

我不知道你的方法行得通 - MvvmCross 中的 res: 方案用于处理位图 - 请参阅 https://github.com/MvvmCross/MvvmCross/blob/900b6dd6a837e97c7204f832c4c6344d05bcf582/Plugins/Cirrious/DownloadCache/Cirrious.MvvmCross.Plugins.DownloadCache.Droid/MvxAndroidLocalFileImageLoader.cs#L35

中的源代码

对于您的特定示例,使用自定义 属性 或自定义绑定可能更容易 - 例如继承自 Android 的 ImageView 并添加一个 属性:

   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);
      }
   }