xamarin.ios - create or inject ViewModel when building a "tabs" application -


we try build application few tabs. reference-project use example: http://slodge.blogspot.co.uk/2013/06/n25-tabs-n1-days-of-mvvmcross.html

to viewmodel-instances need create tabs, used "homeviewmodel"-pattern mentioned in post: create view model using mvvmcross built in factory?

what don't @ approach initialisation of viewmodel's "new". far understand, skips whole viewmodel-lifecycle (https://github.com/slodge/mvvmcross/wiki/view-model-lifecycle) like. in our current project, we'd use "start()" lifecycle-method, it's never called due initialisation "new".

what worked go way:

        var loaderservice = mvx.resolve<imvxviewmodelloader>();          var vm = (userlistviewmodel)loaderservice.loadviewmodel( new mvxviewmodelrequest(typeof(userlistviewmodel), null, null, null), null); 

so question: way job or dirty workaround , there better solution?

update: came solution:

    createtabfor<settingsviewmodel>("settings", "settings");       //this method loads viewmodel     private uiviewcontroller createtabfor<ttargetviewmodel>(string title, string imagename)           ttargetviewmodel : class, imvxviewmodel     {         var controller = new uinavigationcontroller();         controller.navigationbar.tintcolor = uicolor.black;          var viewmodelrequest = new mvxviewmodelrequest (typeof(ttargetviewmodel), null, null, null);         var screen = this.createviewcontrollerfor<ttargetviewmodel> (viewmodelrequest)  uiviewcontroller;         settitleandtabbaritem(screen, title, imagename);         controller.pushviewcontroller(screen, false);         return controller;     } 

the 'viewmodel lifecycle' area of conflicting interests in mvvmcross. root cause conflict between:

  • viewmodel's models view
  • viewmodel's used within 'showviewmodel' navigation process

for simple 'whole page' user experiences, c-i-r-s viewmodel lifecycle easy support , ensure gets consistently used.

however, user experience starts merge in tabs, flyouts, hamburger menus, dialogs, split views, etc then:

  1. the developers want control viewmodel lifecycles themselves
  2. it's not easy framework ensure view models created, activated , tombstoned/rehydrated consistently

personally, approach - of trying ensure viewmodels independent , constructed same way - mvvmcross doesn't force approach on developers.

specifically tabs, of existing examples use 'owned sub-viewmodel' pattern you've identified.

however, should relatively easy implement other mechanisms if want - have.

in particular, can:

  • use loaderservice directly - getting hold of via mvx.resolve<imvxviewmodelloader>();
  • use showviewmodel custom presenter create both views , viewmodels - beginnings of illustrated in n=25 video take further , add tabs in response showviewmodel calls.
  • use alternative calls create child tabs , viewmodels inside views - e.g. touch sample calls

       var screen = this.createviewcontrollerfor(viewmodel) uiviewcontroller; 

    this replace like:

       var screen = this.createviewcontrollerfor<childviewmodel>() uiviewcontroller;; 

    (or 1 of other overloads mvxcancreatetouchviewextensionmethods.cs)

one repo know users have taken of these ideas , played them sliding menu repo - think have chosen use this.createviewcontrollerfor<tviewmodel> create view models. may or may not way choose go - might of interest experiment with.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -