MonoTouch MVVMCross binding to instance variables

2019-02-28 13:42发布

问题:

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

回答1:

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...