Ami*_*mit 3 fluent mvvm mvvmcross xamarin
我有一个看起来像这样的对象结构。
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];
}
}
Run Code Online (Sandbox Code Playgroud)
我需要使用 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);
}
}
Run Code Online (Sandbox Code Playgroud)
有可能做这样的事情吗?
是否有可能...
是的,MvvmCross 具有很强的可扩展性,因此您可以根据需要添加它。
为此,您需要:
这听起来似乎很费力——但实际上是完全可行的。有关如何INotifyChanged使用插件添加源绑定的示例,请参阅https://github.com/slodge/MvvmCross/tree/v3/Plugins/Crillous/FieldBinding/Cirrious.MvvmCross.Plugins.FieldBinding - 尽管请注意这是无需向 FluentBinding 或解析器添加任何新要求即可实现。
或者...
您可以只在“普通属性”或字段上使用字符串索引器绑定(如果使用 FieldBinding 插件)。
有关 Touch 和 Droid 中的示例,请参阅https://github.com/slodge/MvvmCross-Tutorials/tree/master/ApiExamples 中的 ObservableDictionary 示例
核心项目包括从http://blogs.microsoft.co.il/blogs/shimmy/archive/2010/12/26/observabledictionary-lt-tkey-tvalue-gt-c.aspx复制的 ObservableDictionary 实现(普通字典可以如果不需要动态更新,则使用)
Touch UI 项目包括一个流畅的绑定块:
var set = this.CreateBindingSet<ObservableDictionaryView, ObservableDictionaryViewModel>();
set.Bind(label1).To(vm => vm.Items["One"]);
set.Bind(label2).To(vm => vm.Items["Two"]);
set.Bind(label3).To(vm => vm.Items["Three"]);
set.Bind(all).To(vm => vm.ReplaceAllCommand);
set.Bind(each).To(vm => vm.ReplaceEachCommand);
set.Bind(makeNull).To(vm => vm.MakeNullCommand);
set.Apply();
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
2715 次 |
| 最近记录: |