iphone - How to listen DataContext change to adjust UI state accordingly -
this question taking roots discussion inside https://github.com/slodge/mvvmcross/issues/367 thread.
if we're following recent examples of setting bindings mvvmcross framework setup bindings control properties (like uilabel.text) using bind(), to() extensions:
this.delaybind(() => {     var set = this.createbindingset<kittencollectioncell, kitten>();     set.bind(namelabel).to(kitten => kitten.name);     set.bind(pricelabel).to(kitten => kitten.price);     set.bind(_loader).to(kitten => kitten.imageurl);     set.apply(); });     but if want adjust ui state basing on values datacontext. example show/hide buttons , labels , adjust constraint values.
if (((userinfo)datacontext).isadmin) {     savebutton.hidden = false;     securitylabelconstraint.constant = 50;      /* etc */ }   how can listen datacontext change event? what's recommended approach here?
thanks.
there several way achieve this.
one of simplest way you've identified - e.g. bind securitylabelconstraint directly - e.g. using tibet binding:
set.bind(securitylabelconstraint)    .for(p => p.constant)    .to("if(info.isadmin, 20, 0)");   alternatively if there complicated logic needed (beyond scope or performance of tibet binding engine) can implement binding property on cell - or on intermediate object.
for example, have wrapper class implements:
 public class wrapper  {      private nslayoutconstraint _constraint;      public wrapper(nslayoutconstraint constraint)      {          _constraint = constraint;      }       private string _foo;      public string foo      {         { return _foo; }         set         {            _foo = value;            // complex logic on _constraint goes here         }      }  }   if store _wrapper instance in field on cell, can bound as:
set.bind(_wrapper)    .for(p => p.foo)    .to(info => info.isadmin);   if had instead put foo property on cell, have used:
set.bind()    .for(cell => cell.foo)    .to(info => info.isadmin);   one final way achieve binding try hook cell's datacontext directly , use propertychanged handler - don't recommend tends bit fiddly maintain , it's easy create memory leaks when accessing nsobject's inside event callbacks.
Comments
Post a Comment