3
votes

I am creating something similar to Stuart's AddressUIView used in N=32 - The Truth about ViewModels... starring MvxView on the iPad - N+1 days of MvvmCross

In the ctor I create some UI and call DelayBind, similar to the Tutorial

public CustomerBannerView()
{
    BackgroundColor = UIColor.Green;

    var nameLabel = new UITextView();
    nameLabel.BackgroundColor = UIColor.Blue;
    nameLabel.Text = "Some Text";
    this.Add(nameLabel);

    var numberLabel = new UITextView();
    numberLabel.BackgroundColor = UIColor.Yellow;
    this.Add(numberLabel);

    this.DelayBind(
        () =>
            {
                var set = this.CreateBindingSet<CustomerBannerView, CustomerViewModel>();
                set.Bind(nameLabel).To(vm => vm.Name);
                set.Bind(numberLabel).To(vm => vm.Number);
                set.Apply();
            });

    this.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();

    this.AddConstraints(
        nameLabel.AtLeftOf(this, 10),
        nameLabel.AtTopOf(this, 10),
        numberLabel.AtRightOf(this, 10),
        numberLabel.AtTopOf(this, 10));

}

In the tutorial the DataContext property of the MvxView is bound to a Property on an Outer/Parent ViewModel. In many situations including mine the Parent Property will be Null and then at a subsequent data a valid instance.

This means that when the outer binding is initially applied it sets the MvxView's DataContext to Null. DelayBind fires and the following warnings are output

MvxBind:Warning: 23.37 Unable to bind: source property source not found Property:Name on null-object [0:] MvxBind:Warning: 23.37 Unable to bind: source property source not found Property:Number on null-object

Once the Parent Property is set to a valid instance the binding does push the new value through with no problem?

  1. Am I using DelayBind and the DataContext property in a way that is not expected?
  2. Is it worth considering a change to MVVMCross to not call DelayBind if the DataContext is not changing? i.e. Null -> Null is not a change
1
Can you show the code for CustomerViewModel? It looks like part of the problem may be there; Name doesn't appear to be visible to the binding.cdbitesky

1 Answers

1
votes
  1. You are not using DelayBind in an unexpected way. What you should consider is to avoid firing property changes when the value remains the same (I can recommend you to use Fody.PropertyChanged, which will automatically take care of that).

  2. I don't think so, since the way it currently works gives the developer more power/freedom. Bindings are responsible for keeping the UI updated based on DataContext changes, all logic about firing / not firing changes is responsibility of the DataContext itself.