将IBitmap绑定到ImageView

时间:2015-03-11 14:35:10

标签: xamarin xamarin.android reactiveui

我尝试将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中“转换”它,如下所示:this.OneWayBind(this.ViewModel, vm => vm.WeatherIcon, v => v.weatherImageView.Drawable, v => v.ToNative());

但这也不起作用。我还是ReactiveUI的新手,所以有点暗示会很好。

1 个答案:

答案 0 :(得分:0)

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

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

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

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

相关问题