ios - ViewModel is null during ViewDidLoad -
i getting started mvvmcross in ios.
public class mainview : mvxtabbarviewcontroller { public override void viewdidload() { base.viewdidload(); var vm = (mainviewmodel)this.viewmodel; if (vm == null) return; } }
setting breakpoint line access viewmodel
, shows me, viewmodel
null.
i can workaround calling viewdidload()
in constructor. then, viewmodel
null during constructor call, valid in default viewdidload
call. looks workaround. can help?
i'm guessing here problem here specific way tabbarviewcontroller
constructed.
viewdidload
virtual method , called first time view accessed.
in case of tabbarviewcontroller
happens during ios base view constructor - i.e. occurs before class has had constructor called.
the way around i've found add check against situation in viewdidload
, , make second call viewdidload
during class constructor.
you can see in action n-25 - https://github.com/mvvmcross/nplus1daysofmvvmcross/blob/976ede3aafd3a7c6e06717ee48a9a45f08eedcd0/n-25-tabbed/tabbed.touch/views/firstview.cs#l17
something like:
public class mainview : mvxtabbarviewcontroller { private bool _constructed; public mainview() { _constructed = true; // need additional call viewdidload because uikit creates view before c# hierarchy has been constructed viewdidload(); } public override void viewdidload() { if (!_constructed) return; base.viewdidload(); var vm = (mainviewmodel)this.viewmodel; if (vm == null) return; } }
Comments
Post a Comment