Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the best practices for handling navigation in MvvmCross


What are the best practices for handling navigation in MvvmCross


Handling navigation in MvvmCross involves several best practices that ensure efficient and platform-agnostic navigation between view models. MvvmCross uses a ViewModel-first approach, meaning navigation is managed at the view model level rather than directly between views. Here are some key practices to follow:

1. Use `IMvxNavigationService`**

The `IMvxNavigationService` is the core component for navigating between view models in MvvmCross. It abstracts native platform navigation, allowing you to navigate from one view model to another without specifying the view. This service supports asynchronous navigation methods, such as `Navigate()`, which can be used to move to a new view model without manually specifying the associated view[1][2].

2. Navigate with Parameters**

If you need to pass parameters to the target view model, use the `Navigate()` method. Ensure the target view model implements `IMvxViewModel` and overrides the `Init(TParameter parameter)` method to handle the passed parameters[2][4].

3. Handle Navigation Events**

MvvmCross provides several navigation events that you can hook into, such as `BeforeNavigate`, `AfterNavigate`, `BeforeClose`, and `AfterClose`. These events allow you to intercept and react to navigation changes, which can be useful for logging, analytics, or other business logic[4].

4. Use URI Navigation for Deep Linking**

For more complex scenarios, such as deep linking or handling multiple navigation paths, MvvmCross supports URI navigation. This involves defining navigation attributes on view models and using URIs to navigate between them. This approach also enables features like hamburger, tab, or top navigation structures[4].

5. Manage Navigation Stack**

When navigating through multiple view models, consider how the navigation stack is managed. MvvmCross does not directly support modifying the navigation stack like some other frameworks, so you may need to close and reopen view models to achieve the desired stack structure[7].

6. Avoid Displaying Intermediate Views**

If you need to navigate through multiple view models without displaying intermediate views, consider closing the current view model and then navigating to the final one. However, this might briefly show intermediate views, which can be distracting. There is no built-in way in MvvmCross to completely avoid displaying these views during transitions[7].

Example of Navigation

Here's a simple example of how to navigate from one view model to another using `IMvxNavigationService`:

csharp
using MvvmCross.Navigation;
using MvvmCross.ViewModels;

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

        public FirstViewModel(IMvxNavigationService navigationService)
        {
            _navigationService = navigationService;
        }

        public IMvxAsyncCommand NavigateCommand => new MvxAsyncCommand(async () =>
        {
            await _navigationService.Navigate();
        });
    }

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

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

Conclusion

MvvmCross simplifies cross-platform navigation by abstracting away platform-specific details, allowing developers to focus on view model-level navigation. By following these best practices, you can efficiently manage navigation in your MvvmCross applications.

Citations:
[1] https://www.mvvmcross.com/documentation/tutorials/tipcalc/the-tip-calc-navigation
[2] https://yanxiaodi.gitbook.io/xamarin-mvvmcross-handbook/using-mvvmcross-to-create-your-first-xamarin-app/navigation/understanding-the-imvxnavigationservice
[3] https://www.reddit.com/r/androiddev/comments/9g49bq/mvvm_question_about_handling_click_and_navigation/
[4] https://www.mvvmcross.com/documentation/fundamentals/navigation
[5] https://www.mvvmcross.com/documentation/getting-started/getting-started
[6] https://stackoverflow.com/questions/22142120/mvvmcross-and-complex-navigation
[7] https://stackoverflow.com/questions/51474717/mvvmcross-navigation-through-multiple-view-models
[8] https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross/Navigation/IMvxNavigationService.cs