MonoTouch MVVMCross 绑定到实例变量 [英] MonoTouch MVVMCross binding to instance variables

查看:32
本文介绍了MonoTouch MVVMCross 绑定到实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来可能很愚蠢,但我无法绑定到实例变量(字符串类型)才能工作.

This probably sounds really stupid, but I just can't get a binding to an instance variable (of type string) to work.

在我视图的 xib 中,我创建了一个文本字段作为 IB 中的出口,然后我可以将它绑定到我的 viewModel 的字符串属性.但是,它不会让我以相同的方式将视图的字符串变量绑定到 viewModel 的属性.

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'}}" },
     });

推荐答案

从阅读你的问题,我认为你说的是​​ View 本身有一个 string 类型的字段...

From reading your question, I think what you are saying is that the View itself has a field of type string...

您的代码:

this.AddBindings(
     new Dictionary<object, string>()
     {
          { StringTemp, "{'Text':{'Path':'AboutText'}}" },
     });

正在尝试将 StringTemp 引用的对象上的属性 Text 绑定到 ViewModel 上 AboutText 中的任何内容.

is trying to bind the property Text on the object referred to by StringTemp to whatever is in AboutText on the ViewModel.

要设置 StringTemp 字符串本身,您应该能够使用以下内容绑定到它:

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'}}" },
      });

<小时>

只是解释一下:{ this, "{'StringTemp':{'Path':'AboutText'}}" },这些可以认为是{ TargetObject, "{'TargetPropertyName':{'Path':'SourcePropertyName'}}" } 其中:


Just to explain the parts in: { this, "{'StringTemp':{'Path':'AboutText'}}" }, these can be thought of as { TargetObject, "{'TargetPropertyName':{'Path':'SourcePropertyName'}}" } where:

  • TargetObject (this) 是您要在其上设置属性值的对象
  • TargetPropertyName (StringTemp) 是您要设置的名称属性
  • SourcePropertyName (AboutText) 是将作为值来源的属性的名称
  • 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

注意 Mvx 使用属性 - 而不是字段 - 所以 private string StringTemp {get;set;} 是可绑定的,但 private string StringTemp; 不是.

Note that Mvx uses Properties - not fields - so private string StringTemp {get;set;} is bindable, but private string StringTemp; is not.

如果您愿意,您也可以为此字符串引用进行双向绑定...但是您需要设置一些自定义绑定信息来这样做 - 需要触发和捕获一些事件才能更新ViewModel(改天再说!)

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!)

对于直接绑定不是您要查找的内容的情况,您可以随时订阅 PropertyChanged 并以更详细的代码处理通知...例如:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆