【问题标题】:iOS MvvmCross CustomBinding for viewiOS MvvmCross CustomBinding 用于视图
【发布时间】:2018-02-12 16:11:34
【问题描述】:

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

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

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

【问题讨论】:

  • 你的问题不清楚。你能分享更多你尝试过的代码吗?
  • 我找到了一个可行的解决方案,我将发布该解决方案,我还将尝试使用简单的图像使我的问题更加清晰。更新将很快跟进!附言。对于投反对票的人,只需写一个像 Cole 这样的评论,而不是单独投反对票……

标签: ios xamarin.ios mvvmcross mvxbind


【解决方案1】:

好的解决办法

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

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

【讨论】:

    猜你喜欢
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多