2013-07-18 44 views

回答

9

好吧,经过很多的斗争,我终于得到了答案。我不得不做下列事情。

1)增加了一个import语句:

using Cirrious.MvvmCross.Binding.BindingContext; 

2)的添加以下代码:

protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 
    SetContentView(Resource.Layout.Hello); 

    TableLayout containerLayout = this.FindViewById<TableLayout>(Resource.Id.containerLayout); 
    if (containerLayout != null) 
    {       
     TableRow newRow = new TableRow(base.ApplicationContext); 
     newRow.SetMinimumHeight(50); 

     var txtRace = new EditText(ApplicationContext); 
     txtRace.Hint = "Race"; 

     var bindingSet = this.CreateBindingSet<HelloView, HelloViewModel>(); 
     bindingSet.Bind(txtRace).To(vm => vm.Race); 
     bindingSet.Apply(); 

     newRow.AddView(txtRace); 
     containerLayout.AddView(newRow); 
    } 
} 

我已经有一个 “TableLayout” 在我HelloView.axml文件和所有我这样做是创建一个新的EditText框控件(txtRace)并将其添加到视图中,同时将其绑定到HelloViewModel对象的“Race”属性。

我花了很多时间试图找出CreateBindingSet()方法存在的命名空间,因为VS2012没有给我任何智力。

希望这可以帮助有人面临类似的问题。

+0

很高兴知道你排序。祝你好运! –

2

是MvvmCross支持在运行时创建的控件的绑定属性。您可以通过他的N + 1系列中令人敬畏的斯图亚特先生观看本教程。 http://www.youtube.com/watch?feature=player_embedded&v=cYu_9rcAJU4

注意:他已经在这个系列中展示过很多次了,但我现在还记得这个在我头上。

+0

感谢Mohib的回应。 – Amit

+0

我仍然在这个问题上。我在看这篇文章:http://stackoverflow.com/questions/16724278/mvvmcross-for-android-how-to-do-binding-in-code。我面临的问题是我无法获得CreateBindingSet()方法。目前我的活动继承自MvxActivity。我是否缺少一些程序集引用或名称空间? – Amit

+0

感谢您的视频链接。斯图尔特先生提到你的部分可以有多个视图模型是有帮助的。 –

相关问题