Correct use of DelayBind in MVVMCross

I am creating something similar to Stuart AddressUIView used in N = 32 - The Truth About ViewModels ... starring MvxView on iPad - N + 1 day 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 in the Outer / Parent ViewModel. In many situations, including mine, the parent property will be Null and then a valid instance in subsequent data.

This means that when the external binding is initially applied, it sets the MvxView DataContext to Null. DelayBind is triggered and the following warnings are displayed.

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

Once the parent property is set to a valid instance, does the binding actually call the new value without issue?

  • Am I using the DelayBind and DataContext property in a way that is not expected?
  • Is it worth considering changing MVVMCross to avoid calling DelayBind if the DataContext doesn't change? those. Null → Null is not a change.
+3


source to share


1 answer


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

  • I don't think so as the way it currently works gives the developer more authority / freedom. Bindings are responsible for keeping the UI updated based on DataContext changes, all firing / failing change logic is responsible for the DataContext itself.



0


source







All Articles