我需要一个自定义绑定(bind),我知道何时何地,但我不知道该怎么做。这是我的自定义绑定(bind)中 View 的关系。想想像控件一样的* View 。
我有来自 ViewModel->ContainerView->FirstView 的连接,但我无法将它与 TableView 连接。为了将 ContainerView 连接到 FirstView,我做了一个自定义绑定(bind)(目前是一个方向)。在 setvalue 方法中,我调用了 firstview 的方法 SetBinding(我想在其中进行绑定(bind))
我尝试了几个选项,但没有任何 react ,最后一个看起来像这样:
public GolferList CurrentGolferList { get; set; }
public void SetBinding(GolferList golferList){
this.CurrentGolferList = golferList;
TableSource = new TableSourcePlayers(TableViewPlayers);
var bindingDescription = new[]{
new MvxBindingDescription {TargetName = "ItemsSource",SourcePropertyPath = "CurrentGolferList"} ,
};
Binder.Bind(this,TableSource, bindingDescription);
TableViewPlayers.Source = TableSource;
TableViewPlayers.ReloadData();
}
如果您能告诉我另一种处理方法,我将不胜感激。
更新 :
我关注了 Stuart 的链接,现在它工作正常,非常感谢 Stuart!
实际上,在我的方案中,TableView 是一个 MvxSimpleBindableTableViewSource,我想在那里绑定(bind)数据。所以为了让它工作,我使用了下面的代码(SetBinding 需要一些外部重构):
private List<IMvxUpdateableBinding> bindings;
private string BindingText = "{'ItemsSource':{'Path':'CurrentGolfers'}}";
public object DataContext {
get { return dataContext; }
set { dataContext = value;
if (bindings == null)
bindings = this.GetService<IMvxBinder>().Bind(dataContext, TableSource, BindingText).ToList();
else
bindings.ForEach(b => b.DataContext = dataContext);
}
}
public void SetBinding(GolferList golferList){
this.DataContext = PlayViewModel;
tableView.Source = TableSource;
tableView.ReloadData();
}
请注意,BindingText 指向表,而不是 View 本身。
更新 2
现在在 V3 中有点不同。首先, View 必须实现 IMvxBindable 和这个成员:
public object DataContext
{
get { return BindingContext.DataContext; }
set { BindingContext.DataContext = value; }
}
public IMvxBindingContext BindingContext { get; set; }
(不要忘记调用 BindingContext.ClearAllBindings() 并在 viewload 中调用 CreateBindingContext() )
然后你就可以在你的类里面绑定(bind)了。就我而言:
var set = this.CreateBindingSet<FirstPlayViewController, PlayViewModel>();
set.Bind(source).To(vm => vm.CurrentGolfers).Apply(); //I love the new fluent api :)
最佳答案
我认为您想要做的是实际的数据绑定(bind) View ,而不是自定义绑定(bind)。
这个问题涵盖了这一点 - Custom bindable control in a MvvmCross Touch project
基本上,您需要做的是将“Bindings”和“DataContext”属性的集合添加到您的FirstView
。 .
如果你这样做,那么你应该能够在 FirstView 中进行数据绑定(bind)(到 DataContext),就像在任何普通的 MvvmCross View 中一样。
注意 - 这在 v3 中会更容易做到,因为我们添加了一个 'BindingContext' 对象来帮助这种类型的操作
关于xamarin.ios - mvvmcross 中的海关绑定(bind)交叉 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15526429/