用于iOS UI的MVVM跨图片选择器插件不会在Bytes类型视图模型属性上使用InMemoryImage进行更新

时间:2016-04-18 09:53:58

标签: c# ios xamarin mvvmcross

我正在构建一个跨平台的Xamarin解决方案,使用单独的iOS / android项目和一个包含使用MVVM交叉编写的视图模型的共享PCL(框架和插件的3.5.1版本)。

我正在使用iOS设备相机拍摄照片并将值存储在Bytes变量中,如下所示:

    private byte[] _bytes;

    public byte[] Bytes {
        get { return _bytes; }
        set {
            _bytes = value; 
            RaisePropertyChanged (() => Bytes); 
        }
    }

    public void TakePhoto ()
    {
            var task = Mvx.Resolve<IMvxPictureChooserTask> ();
            task.TakePicture (500, 90,
                stream => {
                    var memoryStream = new MemoryStream ();
                    stream.CopyTo (memoryStream);
                    Bytes = memoryStream.ToArray ();
                },
                () => {
                    // perform any cancelled operation
                });
    }

单击一个绑定按钮可以正确打开相机,并允许我拍照,但是在选择&#34;使用&#34;后,我的UI中的图像视图不会更新。我拍的照片。

这是我的约束力:

        set.Bind(imgPhoto).To(vm => vm.Bytes).WithConversion("InMemoryImage");
        set.Bind (btnTakePhoto).To ("TakePhoto");

imgPhoto在哪里

public MvxImageView imgPhoto { get; set; }

和btnTakePhoto是

public UIButton btnTakePhoto { get; set; }

(我已尽力遵循youtube上MVVMcross N + 17教程中使用的绑定语法 - (Stuart,如果你正在回答这个问题,谢谢你们!)

它在Android上使用相同的视图模型代码并且像这样绑定到视图时非常有效:

local:MvxBind="Bitmap Bytes, Converter=InMemoryImage; Visible Bytes;"

我已经在应用程序的安装类中为内存转换添加了引用程序集(我没有为android做过):

    protected override List<Assembly> ValueConverterAssemblies {
        get {
            var toReturn = base.ValueConverterAssemblies;
            toReturn.Add(typeof(Cirrious.MvvmCross.Plugins.PictureChooser.Touch.MvxInMemoryImageValueConverter).Assembly);
            return toReturn;
        }
    }

我通过nuget添加了picturechooser插件,以便我包含必要的引导代码。

我也试过这个:

set.Bind(imgPhoto).For("Bitmap").To(vm => vm.Bytes).WithConversion("InMemoryImage");

我还尝试使用MvxImageViewLoader并绑定到MvxImageView而不是直接绑定到MvxImageView,但也没有任何欢乐。

最后我还尝试在主线程上调用PictureChooserTask,这也没有用。

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

var Obj = function () { var name = "David" // local variable this.name = "Alex" // name property of each newly generated object return name; } var o = new Obj(); // invoke function as a constructor var p = Obj(); // normal function invocation console.log(o); // {name: "Alex"} console.log(p); // "David" 上,该属性称为UIImageView,您必须绑定:

Image

默认绑定属性为set.Bind(imgPhoto).For(v => v.Image).To(vm => vm.Bytes).WithConversion("InMemoryImage"); 。但ImageUrl转换为MvxInMemoryImageValueConverter。这就是为什么它必须绑定到UIImage