redent84
redent84

Reputation: 19249

MvvmCross: MvxTableViewCell binding from outside

I've created a MvxTableViewCell subclass that I plan to reuse at many points of my App, so I want to delegate the binding task to the creator of the cell.

So far, I got this working based on some N+1 tutorial examples, mainly this code from N=17-Collect a Bull part 3 tutorial.

So at this point, creating a cell looks like this:

return new SubtitleCell("TitleText Name; SubtitleText Subject.Descripcion"))

But I don't really like bindings defined in strings, because I usually refactor a lot and this may cause troubles in the near future, so I decided to declare the binding like a normal bindable view like this:

var cell = new SubtitleCell();
var set = cell.CreateBindingSet<SubtitleCell, SubjectGroup>();
set.Bind().For(c => c.TitleText).To(g => g.Name);
set.Bind().For(c => c.SubtitleText).To(g => g.Subject.Descripcion);
set.Apply();
return cell;

This now works like before and now it's refactor-safe, but it's six times the lines of code that I had before and the binding must be defined when the instance of the cell is already created.

I think that it can be done using MvxBindingDescription because the binding text used in the first example is parsed to a IEnumerable<MvxBindingDescription>, but I was unable to find an example or documentation about it.

Can anyone point me to the right direction?

Thanks.

EDIT: LoC is not my biggest concern, but having to instantiate each cell before defining the binding, I'd be happy to have something like this:

var set = Mvx.CreateBindingSet<SubtitleCell, SubjectGroup>();
set.Bind().For(c => c.TitleText).To(g => g.Name);
set.Bind().For(c => c.SubtitleText).To(g => g.Subject.Descripcion);

So I can apply it later maybe like this:

var cell = new SubtitleCell();
cell.ApplyBindingSet(set);

Is there something that can be done moving this way?

UPDATE: Just noticed that the second binding example fills my log console with this error:

MvxBind:Warning:  0.40 Unable to bind: source property source not found
Cirrious.MvvmCross.Binding.Parse.PropertyPath.PropertyTokens.MvxPropertyNamePropertyToken on null-object

Both samples seems to produce the same behavior, but the warning errors only appear using fluent binding.

Upvotes: 0

Views: 1130

Answers (1)

Stuart
Stuart

Reputation: 66882

MvxBindingDescription won't really help with refactoring - it operates just above the text or fluent bindings and just below the reflection level of the binding engine.

If you want Expression based binding for refactoring, then you have to use Fluent binding and pay the penalty of the extra lines of code (if you consider that a penalty)

Upvotes: 1

Related Questions