我有一个应用程序,其中大多数控件是在代码中创建的,然后使用AddView方法添加到布局中.框架是否允许使用代码将ViewModel属性绑定到控件,或者只能在axml文件中完成?
Ami*_*mit 10
好吧,经过很多努力,我终于得到了答案.
我必须做以下事情.
1)添加了一个import语句:
using Cirrious.MvvmCross.Binding.BindingContext;
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在HelloView.axml文件中有一个"TableLayout",我在这里做的就是创建一个新的EditText框控件(txtRace)并将其添加到视图中,同时将其绑定到"Race"属性HelloViewModel对象.
我花了很多时间试图找出存在什么命名空间的CreateBindingSet()方法,因为VS2012没有给我任何关于它的智能.
希望这有助于面临类似问题的人.
| 归档时间: |
|
| 查看次数: |
2775 次 |
| 最近记录: |