MVVMCross中DelayBind的正确使用方法

3

我正在创建类似于Stuart在N=32 - The Truth about ViewModels... starring MvxView on the iPad - N+1 days of MvvmCross中使用的AddressUIView。

在构造函数中,我创建了一些UI并调用了DelayBind,类似于教程中的操作。

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));

}

在本教程中,MvxView的DataContext属性绑定到外部/父ViewModel上的一个属性。在包括我的许多情况下,父属性将为Null,然后在随后的数据中为有效实例。
这意味着当外部绑定最初应用时,它将MvxView的DataContext设置为Null。DelayBind触发并输出以下警告:
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
一旦父属性设置为有效实例,绑定确实可以顺利地推送新值吗?
以下是需要回答的问题:
1. 我是否以不符合预期方式使用DelayBind和DataContext属性? 2. 是否值得考虑对MVVMCross进行更改,以便在DataContext未更改时不调用DelayBind?即Null-> Null不是更改。

您能展示一下CustomerViewModel的代码吗?问题的一部分可能出在那里;Name似乎对于绑定不可见。 - cdbitesky
1个回答

1
  1. 你没有以意外的方式使用DelayBind。你应该考虑避免在值保持不变时触发属性更改(我建议你使用Fody.PropertyChanged,它会自动处理这个问题)。

  2. 我不这么认为,因为它当前的工作方式给开发人员提供了更多的权力/自由。绑定负责根据DataContext的更改更新UI,所有关于触发/不触发更改的逻辑都是DataContext本身的责任。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接