将 IBitmap 绑定到 ImageView

Bind IBitmap to ImageView

我尝试将 ViewModel 中的 IBitmap 绑定到 AndroidActivity 中的 ImageView。基本绑定不起作用,要求我为此注册一个 IBindingTypeConverter

class BitmapToImageViewConverter : IBindingTypeConverter
{
    public int GetAffinityForObjects(Type fromType, Type toType)
    {
        return (fromType == typeof (IBitmap) && toType == typeof (ImageView)) ? 2 : 0;
    }

    public bool TryConvert(object from, Type toType, object conversionHint, out object result)
    {
        if (from == null)
        {
            result = null;
            return false;
        }

        Drawable drawable = ((IBitmap) from).ToNative();
        ImageView test = new ImageView(WeatherApp.AppContext);
        test.SetImageDrawable(drawable);

        result = test;
        return true;
    }
}

那没用。所以我试图像这样在 OneWayBind 中 "convert" 它:this.OneWayBind(this.ViewModel, vm => vm.WeatherIcon, v => v.weatherImageView.Drawable, v => v.ToNative());

但这也没有用。我还是 ReactiveUI 的新手,所以有点提示会很好。

所以和往常一样,这是程序员的问题。 :D 这就是我现在做的方式,我对这个解决方案很满意。我错过了 using System;,这让 C# 从另一个库中获取了 Subscribe 方法。

this.WhenAnyValue(activity => activity.ViewModel.WeatherIcon)
    .Subscribe(image =>
    {
        if (image == null)
            return;

        WeatherImageView.SetImageDrawable(image.ToNative());
    });

但我仍然很好奇是否有办法使用 IBindingTypeConverter 解决这个问题。