MonoTouch的MVVMCross结合实例变量 [英] MonoTouch MVVMCross binding to instance variables
问题描述
这可能听起来很愚蠢,但我只是不能获得绑定到一个实例变量(字符串类型)上班。
在我看来的厦门国际银行,我创建一个文本字段作为IB的插座,那么我就可以把它绑定到我的viewmodel中的字符串属性。但是,它不会让我,我的观点的字符串变量视图模型的属性绑定以同样的方式。
有谁知道这是由设计,还是我失去了一些东西?结合code是 -
this.AddBindings(
新字典<对象,字符串>()
{
{TextFieldTemp,{文本:{路径:AboutText'}}},
});
从阅读你的问题,我想你说的是,查看本身有一个string类型的领域...
您code:
this.AddBindings(
新字典<对象,字符串>()
{
{StringTemp,{文本:{路径:AboutText'}}},
});
正试图通过 StringTemp
属性文本
所指的对象绑定到无论是在 AboutText
的视图模型。
要设置 StringTemp
字符串本身,你应该能够使用类似绑定到它:
this.AddBindings(
新字典<对象,字符串>()
{
{这一点,{'StringTemp:{路径:AboutText'}}},
});
只是为了解释部分中: {这一点,{'StringTemp:{路径:AboutText'}}}
,这可以被认为是为 {TargetObject{'TargetPropertyName:{路径:SourcePropertyName'}}}
其中:
- TargetObject(
这个
)是您的目标是要设置属性值对象 - TargetPropertyName(
StringTemp
)是你的目标设定name属性 - SourcePropertyName(
AboutText
)是属性的名称将成为价值的源泉
注意MVX使用属性 - 不是场 - 这样私人字符串StringTemp {获取;集;}
是绑定的,但私人字符串StringTemp;
不是
您也可以做双向此字符串引用绑定,如果你想......但你会需要设置一些自定义的绑定信息这样做 - 那里将需要一些事件触发,以捕获更新视图模型(我会留给另一天!)
有关,其中直接结合的情况下是不是你在找什么,那么你可以随时订阅的PropertyChanged和处理更详细的code中的通知...例如:
ViewModel.PropertyChanged + =(S,E)=>
{
如果(e.PropertyName ==AboutText)
{
//做一些新ViewModel.AboutText值这里复杂
}
};
...但我个人倾向于避免这种类型的code在那里我可以...
This probably sounds really stupid, but I just can't get a binding to an instance variable (of type string) to work.
In my view's xib, I create a text field as an outlet in IB, then I can bind it to my viewModel's string property. However, it won't let me bind my view's string variable to the viewModel's property in the same way.
Does anyone know if this is by design, or am I missing something? The binding code is -
this.AddBindings(
new Dictionary<object, string>()
{
{ TextFieldTemp, "{'Text':{'Path':'AboutText'}}" },
});
From reading your question, I think what you are saying is that the View itself has a field of type string...
Your code:
this.AddBindings(
new Dictionary<object, string>()
{
{ StringTemp, "{'Text':{'Path':'AboutText'}}" },
});
is trying to bind the property Text
on the object referred to by StringTemp
to whatever is in AboutText
on the ViewModel.
To set the StringTemp
string itself you should be able to bind to it using something like:
this.AddBindings(
new Dictionary<object, string>()
{
{ this, "{'StringTemp':{'Path':'AboutText'}}" },
});
Just to explain the parts in: { this, "{'StringTemp':{'Path':'AboutText'}}" }
, these can be thought of as { TargetObject, "{'TargetPropertyName':{'Path':'SourcePropertyName'}}" }
where:
- TargetObject (
this
) is the object you are aiming to set property values on - TargetPropertyName (
StringTemp
) is the name property that you are aiming to set - SourcePropertyName (
AboutText
) is the name of the property that will be the source of the value
Note that Mvx uses Properties - not fields - so private string StringTemp {get;set;}
is bindable, but private string StringTemp;
is not.
You could also do two-way binding for this string reference if you wanted to... but you would need to setup some custom binding information to do so - there would need to be some event fired and captured in order to update the ViewModel (I'll leave that for another day!)
For situations where direct binding isn't what you are looking for, then you can always subscribe to PropertyChanged and handle the notifications in more verbose code... e.g.:
ViewModel.PropertyChanged += (s,e) =>
{
if (e.PropertyName == "AboutText")
{
// do something complicated here with the new ViewModel.AboutText value
}
};
...but I personally tend to avoid this type of code where I can...
这篇关于MonoTouch的MVVMCross结合实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!