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?
- Am I using DelayBind and the DataContext property in a way that is not expected?
- 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
CustomerViewModel
? It looks like part of the problem may be there;Name
doesn't appear to be visible to the binding. – cdbitesky