iosxamarin.iosmvvmcrossmvxbind

iOS MvvmCross CustomBinding for view


I have a custom control, override of UIScrollView. When rendering two UIViews are rendered. One is the PDF, one is a layer above. The layer above contains a collection of texts which rendered at different places over the pdf.

I use MvvmCross so have a model. Collection of texts is observable. How can I bind the observable collection to the results which need to be rendered in the layer?

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>)

I tried:

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

The LayerView is MvxView for bindingcontext


Solution

  • Ok the solution

    The own implementation of the scrollview will be:

    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();
    }
    

    And the place where you are using the ScrollView will be a binding to the 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. I will implement this in MvvmCross by own control. However, this can be used for all own controls!