2
votes

I have implemented a TabBarController much like the Conference tutorial for iOS. Everything is working well now except it appears that the Init methods on Viewmodels for the tabs never get called as it normally would.

Is there something I forgot to do to ensure that Init is called? If I must, I could move this code to the constructor of the ViewModel, but I'd like to avoid this if possible.

EDIT

I guess what I'm really asking here is if I manually instantiate a viewmodel and "create" a view from it via the extension/helper methods, will init get called at some point in the process? If so, at what point can I expect init to get called on the viewmodel?

1

1 Answers

4
votes

The Construction-Initialize-ReloadState-Start (C-I-R-S) sequence is called on an MvxViewModel if it is created using the default ViewModel locator - which is what happens when you call ShowViewModel.

If the ViewModels for the sub-tabs are created by calling new on a ViewModel directly - like in the Conference HomeViewModel.cs#L11 - then obviously this same sequence doesn't happen.

Really the difference here is between:

  • the tab ViewModels which are just INotifyPropertyChanged objects
  • the page-level MvxViewModels which are also INotifyPropertyChanged objects, but which you further expect to be initialized within navigations.

If you wanted to unify the two concepts then you probably could... but actually I suspect it might be simpler and cleaner to perhaps give the two class types different names, to just put the init code in the constuctor for the tab view models, or perhaps to just call Init on them yourself in the Home constructor.