Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show view from non-view/viewmodel in mvvmcross

What is the correct way to open a View in mvvmcross from a non-view? From within a viewmodel we would use ShowViewModel<>(..).

Specifically we are responding to a push notification opening the app (with a custom payload) which dictates a view that should be loaded.

We have a hackety workaround just for proof of concept, just wanted to get an idea of the correct MVX approach

like image 970
geoffreys Avatar asked May 21 '13 05:05

geoffreys


1 Answers

I don't think there is a 'correct way' - I think it depends on your app and what you need it to do.

For some specific cases - e.g. ViewModel->ViewModel and AppStart - MvvmCross provides some convenient methods:

  • you can call ShowViewModel in MvxViewModel
  • the app start can be overridden to use a hint object - see https://speakerdeck.com/cirrious/appstart-in-mvvmcross

But overall, any class can request a ShowViewModel by calling:

         var viewDispatcher = Mvx.Resolve<IMvxViewDispatcher>();
         viewDispatcher.ShowViewModel(new MvxViewModelRequest(
                                                    viewModelType,
                                                    parameterBundle,
                                                    presentationBundle,
                                                    requestedBy));

Further, there is a base class - MvxNavigatingObject.cs - which can help with this (it's a base class of MvxViewModel and MvxAppStart) - so you can easily provide one or more services like INavigateMyselfService who's implementations inherit from MvxNavigatingObject.

  public interface INavigateMyselfService
  {
      void GoWild(string side);
  }

  public class NavigateMyselfService
     : MvxNavigatingObject
     , INavigateMyselfService
  {
      public void GoWild(string side)
      {
          ShowViewModel<WildViewModel>(new { side = side });
      }
  }
like image 191
Stuart Avatar answered Nov 17 '22 08:11

Stuart