Mvvmcross(iOS)绑定UIViewController的标题

前端之家收集整理的这篇文章主要介绍了Mvvmcross(iOS)绑定UIViewController的标题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以将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 loaded
Thread started: #12
2014-02-05 17:09:36.219 TerraFlex[9145:907] 02-05-2014 17:09:36:2190 :1: [Fatal] A critical error occurred and the application was closed
2014-02-05 17:09:36.250 TerraFlex[9145:907] 02-05-2014 17:09:36:2497 :1: [Fatal] Date: 5/02/2014 5:09:36 PM,[Exception]
Fatal
Message: Object reference not set to an instance of an object
Stack: at CirrIoUs.MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.TryCreateSpecificFactoryBinding (System.Object target,System.String targetName,IMvxTargetBinding& binding) [0x00001] in c:\Projects\Misc\MVVMCROSS\CirrIoUs\CirrIoUs.MvvmCross.Binding\Bindings\Target\Construction\MvxTargetBindingFactoryRegistry.cs:69
at CirrIoUs.MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.CreateBinding (System.Object target,System.String targetName) [0x00001] in c:\Projects\Misc\MVVMCROSS\CirrIoUs\CirrIoUs.MvvmCross.Binding\Bindings\Target\Construction\MvxTargetBindingFactoryRegistry.cs:22
at CirrIoUs.MvvmCross.Binding.Bindings.MvxFullBinding.CreateTargetBinding (System.Object target) [0x00003] in c:\Projects\Misc\MVVMCROSS\CirrIoUs\CirrIoUs.MvvmCross.Binding\Bindings\MvxFullBinding.cs:135
at CirrIoUs.MvvmCross.Binding.Bindings.MvxFullBinding..ctor (CirrIoUs.MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00014] in 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] in c:\Projects\Misc\MVVMCROSS\CirrIoUs\CirrIoUs.MvvmCross.Binding\Binders\MvxFromTextBinder.cs:55
at CirrIoUs.MvvmCross.Binding.Binders.MvxFromTextBinder+<>c_DisplayClass1.b_0 (CirrIoUs.MvvmCross.Binding.Bindings.MvxBindingDescription description) [0x00000] in c:\Projects\Misc\MVVMCROSS\CirrIoUs\CirrIoUs.MvvmCross.Binding\Binders\MvxFromTextBinder.cs:37
at System.Linq.Enumerable+c__Iterator102[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 bindings,System.Object clearKey) [0x00028] in c:\Projects\Misc\MVVMCROSS\CirrIoUs\CirrIoUs.MvvmCross.Binding\BindingContext\MvxBindingContextOwnerExtensions.cs:69
at CirrIoUs.MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings (IMvxBindingContextOwner view,System.Object target,IEnumerable1 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,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
at CirrIoUs.MvvmCross.Binding.BindingContext.MvxFluentBindingDescriptionSet`2[Trimble.TFM.FieldApp.HomeView,Trimble.TFM.FieldApp.Common.Homeviewmodel].Apply () [0x00000] in :0

谢谢,
乔恩

解决方法

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

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

> https://github.com/MvvmCross/MvvmCross/wiki/Databinding#wiki-fluent

你的约束力是:

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

所以你有约束力:

> .Bind(Title) – 当前Title对象中的任何内容(通常为null)
>对于该对象的默认绑定属性(取决于对象类型),请参阅https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/MvxTouchBindingBuilder.cs#L104作为基本列表
>到DataContext上的ProjectName(通常是您的viewmodel)

我想你想要的是:

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

我想你也可以这样做:

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

话虽如此,Mvx堆栈不应该真正禁止具有空目标和属性 – 它不习惯 – 所以我将这个作为一个问题添加到调查并添加回归测试为什么TryCreateSpecificFactoryBinding不能处理null.

原文链接:https://www.f2er.com/iOS/332514.html

猜你在找的iOS相关文章