Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mvvmcross(iOS) binding the Title of UIViewController

Tags:

mvvmcross

Is it possible to bind the Title of a UIViewController to a ViewModel string property? It never seems to update. As a side issue, if you haven't set the title to something before the bind then there's a crash in the Mvvmcross code when set.Apply() is called.

The UIViewController has no .xib, I'm not sure if that's part of the problem.

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

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

Here's the stack trace

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, IEnumerable1 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, 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.MvxBaseFluentBindingDescription1[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

Thanks, Jon

like image 707
user3264007 Avatar asked Feb 04 '14 06:02

user3264007


1 Answers

Fluent binding uses code like:

 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

You can read more about this at:

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

Your binding is:

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

So you are binding:

  • .Bind(Title) - whatever is in the current Title object (which will often be null)
  • for whatever the default binding property is of that object - which depends on the object type - see https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/MvxTouchBindingBuilder.cs#L104 for the basic list
  • to ProjectName on your DataContext (normally your ViewModel)

I guess what you wanted was:

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

I think you can also do this as:

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

With that said, the Mvx stack shouldn't really have barfed at having a null target and property - it didn't used to - so I'll add this as an issue to investigate and add a regression test for why TryCreateSpecificFactoryBinding can't handle null.

like image 111
Stuart Avatar answered Jan 13 '23 13:01

Stuart