gpt4 book ai didi

mvvmcross - 在 MVVMCross 中正确使用 DelayBind

转载 作者:行者123 更新时间:2023-12-03 08:12:43 29 4
gpt4 key购买 nike

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

在 ctor 中,我创建了一些 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 属性绑定(bind)到外部/父 ViewModel 上的属性。在包括我在内的许多情况下,父属性将为 Null,然后在后续数据中为有效实例。

这意味着当最初应用外部绑定(bind)时,它将 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

一旦父属性设置为有效实例,绑定(bind)就会毫无问题地推送新值?

  1. 我是否以意想不到的方式使用了 DelayBind 和 DataContext 属性?
  2. 如果 DataContext 没有变化,是否值得考虑更改 MVVMCross 以不调用 DelayBind?即 Null -> Null 不是变化

最佳答案

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

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

关于mvvmcross - 在 MVVMCross 中正确使用 DelayBind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27925522/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com