使用 Xamarin.iOS 和 MvvmCross 绑定到 MvxView 时出现问题

Problem with binding to MvxView with Xamarin.iOS and MvvmCross

我在使用 Xamarin.iOS 和 MvvmCross (6.2.3.0) 实现以下场景时遇到问题。我有一个视图,在它的视图模型中我有一个 属性 这是一个对象列表。我需要为视图中的每个列表条目动态生成标签和文本字段。所以我决定为此实现一个自定义视图。到目前为止,这是我尝试过的:

在我的 viewcontroller 中,我在 CreateView 中简单地添加了我的自定义 UIView。我可以在我的视图中看到它的内容。在 ViewDidLoad() 我创建绑定:

private void CreateBindings()
{
var set = this.CreateBindingSet<MyController, MyViewModel>();
set.Bind(myCustomControl).For(x => x.DataContext).To(vm => vm.MyListOfObjects);
set.Apply();
}

MyCustomControl代码如下:

public class MyCustomControl : MvxView
{
public MyCustomControl() {

//DataContext is always null here!
//I'd like to get access to list of objects here, add controls for each entry and make sure they are binded to a viewmodel's list of objects somehow.
}

}

我注意到我的视图模型中的对象列表设置晚于对 MyCustomControl 的构造函数调用,因此 MyCustom 控件中的 DataContext 为空是有道理的。我错过了一些我相信的明显的东西。有人能指出我正确的方向吗?我将不胜感激。

我试过这个例子,这正是我想要实现的,但到目前为止运气不好 ;(

N=32 - iPad - N+1 天的 MvvmCross 上的 ViewModels 和 MvxView https://www.youtube.com/watch?v=cYu_9rcAJU4&list=PLR6WI6W1JdeYSXLbm58jwAKYT7RQR31-W&index=35&t=1649s

查看您发布的视频 23:43。 您应该在 DelayBind 方法中进行视图绑定。

this.DelayBind(() => {
    var set = this.CreateBindingSet<MyCustomControl, MyItemViewModel>();

    set.Bind(badgeLabel).For(v => v.Text).To(x => x.BadgeText);
    set.Bind(topSummary).For(v => v.Text).To(x => x.TopSummary);
    set.Bind(bottomSummary).For(v => v.Text).To(x => x.BottomSummary);

    set.Bind(this.Tap()).For(v => v.Command).To(x => x.Edit);
    set.Apply();
});

MvvmCross 将为您完成绑定。 另请注意,必须将正确的类型传递给 CreateBindingSet。

在侧节点上,MvvmCross N+1 视频来自 2013 年,此后发生了一些变化。 你可以在那里找到一些很好的例子,但有时它不再起作用了。

如果您是MvvmCross新手,请下载Playground项目源码参考:

https://github.com/MvvmCross/MvvmCross/tree/develop/Projects/Playground

如果您需要更多帮助,请加入#mvvmcross slack 频道

https://xamarinchat.herokuapp.com/

MvvmCross 真的很棒...一旦你掌握了基本概念。