Where I should place initialization/binding logic when using MvxTableViewCell, created from Nib?

46 Views Asked by At

I created custom MvxTableViewCell, placed all design layouts in .xib file:

public partial class FxTransactionCell: MvxTableViewCell
{
    public static readonly NSString Key = new NSString("FxTransactionCell");
    public static readonly UINib Nib = UINib.FromName(Key, NSBundle.MainBundle);

    static FxTransactionCell()
    {
    }

    protected FxTransactionCell(IntPtr handle): base(handle)
    {
        // Note: this .ctor should not contain any initialization logic.
    }
}

All samples I've seen placed initialization/binding logic into (IntPtr) constructor, but note that comment placed there by VS. I think this constructor can not hold any init logic, because my custom UI elements not yet created and all my UILabels, UIButtons (which layouts in .xib file) are null inside this constructor. So, where I should place my init/bindings logic then?

1

There are 1 best solutions below

8
fmaccaroni On

In your public constructor you should put your init/binding logic. Pay attention that you have to use DelayBind(...) to do binding inside an MvxTableViewCell

public FxTransactionCell()
{
    // Init views if you need to

    // Create the bindings
    this.DelayBind(this.CreateBindings);
}

public FxTransactionCell(IntPtr handle) : base(handle)
{
    // Some people repeat the same logic here just in case the above ctor does not get called, ignoring the note from VS but I don't think it's necessary.
}

private void CreateBindings()
{
    var set = this.CreateBindingSet<FxTransactionCell, FxTransactionItemViewModel>();
    set.Bind(this.MyLabel).To(vm => vm.MyStringProperty);
    set.Apply();
}

HIH