Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does MvvmCross handle navigation between ViewModels


How does MvvmCross handle navigation between ViewModels


MvvmCross handles navigation between ViewModels using a ViewModel-first approach, where navigation is managed at the ViewModel level rather than the View level. This approach allows for platform-agnostic navigation, meaning you can manage navigation logic in the shared core project without needing to write platform-specific code.

Key Concepts in MvvmCross Navigation

1. ViewModel Navigation: In MvvmCross, you navigate from one ViewModel to another using the `IMvxNavigationService`. This service is injected into your ViewModels, allowing them to request navigation to other ViewModels. For example, you can navigate from `FirstViewModel` to `SecondViewModel` by calling `_navigationService.Navigate()`[1][4].

2. View Presenters: MvvmCross uses View Presenters to manage how ViewModels are presented on the UI. These presenters act as a bridge between the ViewModel and the View layer, handling the presentation logic for each platform. This allows for customization of presentation styles across different platforms (e.g., Android, iOS)[3].

3. Lifecycle Methods: MvvmCross ViewModels have lifecycle methods such as `Init()`, `ReloadState()`, and `Start()`. The `Init()` method is used to initialize the ViewModel with navigation parameters. However, in newer versions of MvvmCross, `Init()` is deprecated in favor of a typed `Initialize()` method for async operations[2][4].

4. Navigation with Parameters: You can pass parameters between ViewModels during navigation. This is achieved by implementing `IMvxViewModel` in the receiving ViewModel and overriding the `Init(TParameter parameter)` method. Parameters are serialized using JSON, so you need to ensure that the MvvmCross JSON plugin is installed or register a custom converter[4].

5. Custom Navigation: MvvmCross allows you to customize navigation by hooking into events provided by the `IMvxNavigationService`, such as `BeforeNavigate`, `AfterNavigate`, `BeforeClose`, and `AfterClose`. This enables you to perform actions at different stages of the navigation process[4].

Example of Navigation

To navigate from `FirstViewModel` to `SecondViewModel`, you would typically do the following:

csharp
using MvvmCross.Commands;
using MvvmCross.Navigation;
using MvvmCross.ViewModels;

namespace MyProject.Core.ViewModels
{
    public class FirstViewModel : MvxViewModel
    {
        private readonly IMvxNavigationService _navigationService;

        public FirstViewModel(IMvxNavigationService navigationService)
        {
            _navigationService = navigationService;
            NavigateCommand = new MvxAsyncCommand(() => _navigationService.Navigate());
        }

        public IMvxAsyncCommand NavigateCommand { get; private set; }
    }

    public class SecondViewModel : MvxViewModel
    {
        // Implementation for SecondViewModel
    }
}

In this example, `FirstViewModel` uses the `IMvxNavigationService` to navigate to `SecondViewModel` when the `NavigateCommand` is executed.

Handling Navigation with Return Values

If you need to navigate through multiple ViewModels and handle return values, you can use the `Close()` method to close the current ViewModel and return to a previous one. However, MvvmCross does not directly support modifying the navigation stack to insert or remove intermediate pages without displaying them briefly. You would typically close the intermediate ViewModel and then navigate forward from the previous ViewModel[12].

Customizing Navigation Behavior

For more complex navigation scenarios, you might need to implement custom logic or override default behaviors. This could involve creating custom View Presenters or modifying the ViewModel lifecycle to suit your application's needs[3][10].

Citations:
[1] https://www.mvvmcross.com/documentation/tutorials/tipcalc/the-tip-calc-navigation
[2] https://stackoverflow.com/questions/29372480/mvvmcross-viewmodel-life-cycle
[3] https://www.mvvmcross.com/documentation/fundamentals/view-presenters
[4] https://www.mvvmcross.com/documentation/fundamentals/navigation
[5] https://www.mvvmcross.com/documentation/getting-started/mvvmcross-overview
[6] https://github.com/MvvmCross/MvvmCross/issues/1979
[7] https://nicksnettravels.builttoroam.com/post-2019-03-24-viewmodel-to-viewmodel-navigation-in-a-xamarinforms-application-with-prism-and-mvvmcross-aspx/
[8] https://www.mvvmcross.com/documentation/tutorials/tipcalc/a-note-about-views-and-viewmodels
[9] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[10] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup
[11] https://gregshackles.com/presenters-in-mvvmcross-navigating-android-with-fragments/
[12] https://stackoverflow.com/questions/51474717/mvvmcross-navigation-through-multiple-view-models