我有一个看起来像这样的对象结构。
public class Model : MvxViewModel
{
private IDictionary<string, string> _properties;
public IDictionary<string, string> Properties
{
get { return _properties; }
}
public string this[string key]
{
get { return Get(key); }
set { Set(key, value); ;}
}
public Model()
{
this._properties = new Dictionary<string, string>();
}
public void Set(string propertyName, string value)
{
if (!_properties.ContainsKey(propertyName))
{
_properties[propertyName].Value = value;
}
}
public string Get(string propertyName)
{
return _properties[propertyName];
}
}
我需要使用 Fluent API 将此对象的信息绑定到控件。我的控件是在代码中创建的。
代码如下所示:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Hello);
Model employeeModel = new Model();
model["Id"] = 1000;
model["FirstName"] = "Stuart";
model["MiddleName"] = "";
model["LastName"] = "Lodge";
TableLayout containerLayout = this.FindViewById<TableLayout>(Resource.Id.containerLayout);
if (containerLayout != null)
{
TableRow newRow = new TableRow(base.ApplicationContext);
newRow.SetMinimumHeight(50);
var txtFirstName = new EditText(ApplicationContext);
txtFirstName.Hint = "First Name";
var bindingSet = this.CreateBindingSet<HelloView, Model>();
bindingSet.Bind(txtFirstName).For("Text").To(vm => vm.Get("FirstName"));
bindingSet.Apply();
newRow.AddView(txtFirstName);
containerLayout.AddView(newRow);
}
}
有可能做这样的事情吗?