Search code examples
androidiosxamarinbindingmvvmcross

Binding in Mvvmcross need the object instanciated?


I bound a text property of a field on a sub-objet of an object. Do i have to instanciate this object to receive the data if the user enter a data ? Or will it be instanciate automatically ?

public class BluetoothLeDevice 
{
    public User User { get; set; }
}

public class User 
{
    public string Lastname { get; set; }
}

public class MyViewModel : MvxViewModel
{
    public BluetoothLeDevice Device { get; set; } = new BluetoothLeDevice();
}

private void CreateBinding(View view)
{
    TextView tv_LastName = view.FindViewById<TextView>(Resource.Id.pair_stepper_user_lastname);

    var set = this.CreateBindingSet<MyView, MyViewModel>();
    set.Bind(tv_LastName).For(v => v.Text).To(vm => vm.Device.User.Lastname);
    set.Apply();
}

Solution

  • Yes you have to instanciate it. MvvmCross does not create complex objects implicitly. It will not crash, but you will see an error/warning in the output/log/trace.