14

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

Stuart
  • 66,722
  • 7
  • 114
  • 165
geoffreys
  • 1,107
  • 7
  • 24

2 Answers2

17

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:

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 });
      }
  }
Stuart
  • 66,722
  • 7
  • 114
  • 165
  • 1
    Ah IMvxViewDispatcher was exactly what I was looking for! INavigateMyselfService is similar to what I'd done, except my implementation was.. not pretty – geoffreys May 21 '13 at 10:34
  • @Stuart thanks for all the work you do, especially in the Xamarin community! This is just perfect. I am using it to handle Unauthorized exceptions allover my app. If I catch such an exception I can now use this for easy navigation to the sign in screen. – dynamokaj Feb 13 '15 at 20:47
  • @Stuart We are seeing random failures to navigate between view models in a scenario where we navigate to SplashScreenViewModel and after an interval to our MainViewModel. Both are using the ShowViewModel(); API. We're ending with a log of "mvx:Diagnostic: 70.22 Showing ViewModel MainViewModel" but the application stays on SplashScreenViewModel. Any hint here how to debug it? Thnx! – baraka Jul 16 '15 at 11:31
0

http://forums.xamarin.com/discussion/4694/conditionally-call-registerappstart-with-mvvmcross

Check the above link and you will get idea

In my case,I want to launch the app from secondary tile.For this,I have to launch specific page for Secondary tile.

My Initial App Start view model is LoginViewModel and my custom app start view model is HomeViewModel.

I controlled this from App.cs (Core) to MyCustomAppStart class.

Refer the above link