简体   繁体   English

MVVMCross Xamarin绑定到带有转换器的位图

[英]MVVMCross Xamarin binding to a bitmap with converter

I have a working BindableViewPager (thanks to Cheesebaron!) and within the viewpager control I have a bitmap for each paged item. 我有一个可正常工作的BindableViewPager(感谢Cheesebaron!),并且在viewpager控件中,我为每个分页的项目都有一个位图。 My viewmodel for the viewpager includes 我的viewpager的viewmodel包括

    private double _bearing;
    public double Bearing
    {
        get { return _bearing; }
        set
        {
            _bearing = value;
            RaisePropertyChanged(() => Bearing);
        }
    }

The intention is to dynamically create a bitmap to be an image that reflects the angle of the bearing. 目的是动态创建一个位图,使其成为反映方位角的图像。 So I bind my ImageView to the bearing with a value converter: 因此,我使用值转换器将ImageView绑定到轴承:

<ImageView
    ...
    local:MvxBind="Bitmap Bearing, Converter=BearingToImage" />



public class BearingToImageConverter : MvxValueConverter<double, byte[]>
{

    protected override byte[] Convert(double value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        Bitmap bmp = BitmapFactory.DecodeResource(Application.Context.ApplicationContext.Resources, Resource.Drawable.direction_pointer_black_bg);
        byte[] bitmapData;
        using (var stream = new MemoryStream())
        {
            bmp.Compress(Bitmap.CompressFormat.Png, 100, stream);
            bitmapData = stream.ToArray();
        }

        return bitmapData;
    }
}

There's a couple of lines of code using a matrix to do the rotation which I've omitted for clarity since the code doesn't work without it either. 有两行代码使用矩阵进行旋转,为清楚起见,我省略了旋转,因为没有它们代码也无法工作。

When running the code, the converter is called but I get a warning: 运行代码时,将调用转换器,但会收到警告:

MvxBind:Warning: 54.71 Value was not be a valid Bitmap

and no image is shown. 并且没有显示图像。 Looking at the PictureTaking example, I see that the binding is done directly against a byte array with an InMemoryImage converter. 查看PictureTaking示例,我看到使用InMemoryImage转换器直接对字节数组进行绑定。 Is it valid to attempt to bind in the way I have done and if I need to use my own converter, do I need to do more to emulate the InMemoryImage conversion? 尝试以我已经完成的方式绑定是否有效?如果我需要使用自己的转换器,是否需要做更多的工作来模拟InMemoryImage转换?

Resolved , and apologies for bad etiquette in answering my own question, but for future reference: 已解决 ,对于在回答我自己的问题时的不良礼节表示歉意,但以供将来参考:

The issue seems to be the missing InMemoryImage converter. 问题似乎是缺少InMemoryImage转换器。 Rather than having my converter return a byte[], I now return an object, that being the bitmap itself. 现在,我返回一个对象,即位图本身,而不是让我的转换器返回byte []。

So the converter signatures become: 因此,转换器签名变为:

public class BearingToImageConverter : MvxValueConverter<double, object> { ...

protected override object Convert(double value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { ////

where the returned object is a Bitmap. 返回的对象是位图。

As I know you can bind the array of bytes to the ImageView but not double, so that's why you have that warning. 据我所知,您可以将字节数组绑定到ImageView但不能将其绑定为double,因此这就是您收到该警告的原因。 And I guess that's why your custom value converter didn't invoke at all. 我想这就是为什么您的自定义值转换器根本不调用的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM