用于视图的iOS MvvmCross CustomBinding

时间:2017-09-04 12:10:41

标签: ios xamarin.ios mvvmcross mvxbind

我有一个自定义控件,覆盖了UIScrollView。 渲染两个UIView时渲染。一个是PDF,一个是上面的一层。 上面的图层包含一系列文本,这些文本在pdf上的不同位置呈现。

我使用MvvmCross,所以有一个模型。可以观察到文本的集合。 如何将可观察集合绑定到需要在图层中呈现的结果?

In short...pseudo

UIScrollViewWrapper
On draw create two layers
layer 1 is pdf image
layer above is view with texts.

Texts need to be bind and observed by Model.Texts (ObservableCollection<string>)

我试过了:

var set = this.CreateBindingSet<ViewWithScrollView, ViewWithScrollViewModel>();
set.Bind(ScrollView.LayerView).For(x => x.LayerItems).To(vm => vm.LayerItems);
set.Apply();

LayerView是MvxView for bindingcontext

1 个答案:

答案 0 :(得分:2)

好的解决方案

scrollview的自身实现将是:

public class MyOwnScrollView : UIScrollView, IUIScrollViewDelegate, IMvxBindable
{
   //Implement the IMvxBindable
   //Add property BindingContext
   public IMvxBindingContext BindingContext { get; set; }

   //Property for holding the datacontext
   [MvxSetToNullAfterBinding]
   public object DataContext
   {
       get { return BindingContext.DataContext; }
       set { BindingContext.DataContext = value; }
   }

   public MyOwnScrollView()
   {
        //Call Mvx for creating the bindingcontext
        this.CreateBindingContext()
   }

   //Create something where you can create the set voor fluent binding with the view model. For example
   public void MyBindingToCreate()
   {
        var set = new new MvxFluentBindingDescriptionSet<AViewInMyScrollView, MyViewViewModel>(AViewInMyScrollView); //Because CreateBindingSet is part of MvxView and UIScrollView is not MvxView
        set.Bind(AViewInMyScrollView).For(x => x.MyPropertyOfView).To(vm => vm.PropertyOfViewModel);
        set.Apply();
}

您使用ScrollView的地方将是对datacontext的绑定

public partial class MyView : MvxView
{
    public MyView() : base("MyView", null)
    {

    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var set = this.CreateBindingSet<MyView, MyViewViewModel>();
        set.Bind(MyOwnScrollView).For(s => s.DataContext).To(vm => vm);
        set.Apply();
    }
}

PS。我将通过自己的控制在MvvmCross中实现它。 但是,这可以用于所有自己的控件!