iOS 视图的 MvvmCross CustomBinding

iOS MvvmCross CustomBinding for view

我有一个自定义控件,覆盖了 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是bindingcontext的MvxView

好的解决方案

滚动视图自己的实现将是:

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 的地方将绑定到数据上下文

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 中实现它。 但是,这可以用于所有自己的控件!