Mvvmcross(iOS)绑定UIViewController的标题

时间:2014-02-04 06:05:04

标签: mvvmcross

是否可以将UIViewController的Title绑定到ViewModel字符串属性?它似乎永远不会更新。作为一个副作用,如果你没有在绑定之前将标题设置为某个东西,那么当set.Apply()被调用时,Mvvmcross代码会出现崩溃。

UIViewController没有.xib,我不确定这是否是问题的一部分。

public override void ViewDidLoad()
{
  base.ViewDidLoad();

  var set = this.CreateBindingSet<HomeView, HomeViewModel>();
  set.Bind(Title).To(vm => vm.ProjectName);
  set.Apply();
}

这是堆栈跟踪

  

2014-02-05 17:09:35.740 TerraFlex [9145:907] 02-05-2014 17:09:35:7403:1:[Info] HomeView已加载   线程开始:#12   2014-02-05 17:09:36.219 TerraFlex [9145:907] 02-05-2014 17:09:36:2190:1:[致命]发生了严重错误,应用程序已关闭   2014-02-05 17:09:36.250 TerraFlex [9145:907] 02-05-2014 17:09:36:2497:1:[致命]日期:5/02/2014 5:09:36 PM,[例外] ]    致命    消息:对象引用未设置为对象的实例    Stack:在c:\ Projects \ Misc \ MVVMCROSS \ Cirrious \ Cirrious中的Cirrious.MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.TryCreateSpecificFactoryBinding(System.Object target,System.String targetName,IMvxTargetBinding&amp; binding)[0x00001]。 MvvmCross.Binding \绑定\目标\建筑\ MvxTargetBindingFactoryRegistry.cs:69     在Cirrious.MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.CreateBinding(System.Object target,System.String targetName)[0x00001]在c:\ Projects \ Misc \ MVVMCROSS \ Cirrious \ Cirrious.MvvmCross.Binding \ Bindings \目标\建筑\ MvxTargetBindingFactoryRegistry.cs:22     在Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.CreateTargetBinding(System.Object target)[0x00003]在c:\ Projects \ Misc \ MVVMCROSS \ Cirrious \ Cirrious.MvvmCross.Binding \ Bindings \ MvxFullBinding.cs:135     在Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding..ctor(Cirrious.MvvmCross.Binding.MvxBindingRequest bindingRequest)[0x00014]在c:\ Projects \ Misc \ MVVMCROSS \ Cirrious \ Cirrious.MvvmCross.Binding \ Bindings \ MvxFullBinding.cs: 60     at Cirrious.MvvmCross.Binding.Binders.MvxFromTextBinder.BindSingle(Cirrious.MvvmCross.Binding.MvxBindingRequest bindingRequest)[0x00001]在c:\ Projects \ Misc \ MVVMCROSS \ Cirrious \ Cirrious.MvvmCross.Binding \ Binders \ MvxFromTextBinder.cs:55     at Cirrious.MvvmCross.Binding.Binders.MvxFromTextBinder +&lt;&gt; c_ DisplayClass1.b _0(Cirrious.MvvmCross.Bindings.Bindings.MvxBindingDescription description)[0x00000]在c:\ Projects \ Misc \ MVVMCROSS \ Cirrious \ Cirrious.MvvmCross.Binding \装订\ MvxFromTextBinder.cs:37     在System.Linq.Enumerable + c__Iterator10 2[Cirrious.MvvmCross.Binding.Bindings.MvxBindingDescription,Cirrious.MvvmCross.Binding.Bindings.IMvxUpdateableBinding].MoveNext () [0x00000] in <filename unknown>:0 at Cirrious.MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings (IMvxBindingContextOwner view, IEnumerable 1个绑定,System.Object clearKey)[0x00028]在c:\ Projects \ Misc \ MVVMCROSS \ Cirrious \ Cirrious.MvvmCross.Binding \ BindingContext \ MvxBindingContextOwnerExtensions.cs:69     在Cirrious.MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings(IMvxBindingContextOwner视图,System.Object目标,IEnumerable 1 bindingDescriptions, System.Object clearKey) [0x00019] in c:\Projects\Misc\MVVMCROSS\Cirrious\Cirrious.MvvmCross.Binding\BindingContext\MvxBindingContextOwnerExtensions.cs:90 at Cirrious.MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBinding (IMvxBindingContextOwner view, System.Object target, Cirrious.MvvmCross.Binding.Bindings.MvxBindingDescription bindingDescription, System.Object clearKey) [0x0000e] in c:\Projects\Misc\MVVMCROSS\Cirrious\Cirrious.MvvmCross.Binding\BindingContext\MvxBindingContextOwnerExtensions.cs:83 at Cirrious.MvvmCross.Binding.BindingContext.MvxBaseFluentBindingDescription 1 [System.String] .Apply()[0x00000] in:0     在Cirrious.MvvmCross.Binding.BindingContext.MvxFluentBindingDescriptionSet`2 [Trimble.TFM.FieldApp.HomeView,Trimble.TFM.FieldApp.Common.HomeViewModel] .Apply()[0x00000] in:0

谢谢, 乔恩

1 个答案:

答案 0 :(得分:16)

Fluent绑定使用如下代码:

 set
    .Bind(<target object for bind>)
    .For(<property on target object>) // leave this out to use the "default binding property for the target object"
    .To(<property/expression on source object>)
    .WithConversion(<converter>, <converter parameter>) // optional
    .OneWay() // optional - also: OneWayToSource(), TwoWay() or OneTime()
    .FallbackValue(<value to use for UnsetValue>(); // optional

您可以在以下网址了解更多相关信息:


你的绑定是:

set.Bind(Title).To(vm => vm.ProjectName);

所以你有约束力:

我想你想要的是:

set.Bind(this).For(v => v.Title).To(vm => vm.ProjectName);

我认为你也可以这样做:

set.Bind().For(v => v.Title).To(vm => vm.ProjectName);

话虽如此,Mvx堆栈实际上不应该有null目标和属性 - 它不习惯 - 因此我会将此作为一个问题添加到调查并添加回归测试TryCreateSpecificFactoryBinding无法处理null的原因。