Search code examples
xamarinmvvmcross

MvvmCross CloseTo ViewModel


In MvvmCross is there a way to ask the navigation service to close to a specific viewModel?

For example let's say I have 3 view models

A B C

I have navigated from A to B to C.

A -> B -> C

In the C view I press a Done button and would like to go back to view A.

Is there a way to do something like this in the C viewModel?

_navigationService.Close<A>(this);


Solution

  • if you are on Xamarin.Forms (if not let me know and I'll update the answer because it's not that simple), in the IMvxNavigationService you have the ChangePresentation(...) method which uses a hint object to tell the framework what you want to do.

     Task<bool> ChangePresentation(MvxPresentationHint hint, CancellationToken cancellationToken = default(CancellationToken));
    

    Here you have the different out-of-the-box hints that the framework provides.

    To address your issue you can just use MvxPopPresentationHint:

    _navigationService.ChangePresentation(new MvxPopPresentationHint(typeof(A)));
    

    or if A is your root ViewModel, you can just use MvxPopToRootPresentationHint:

    _navigationService.ChangePresentation(new MvxPopToRootPresentationHint());
    

    HIH