Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I customize the navigation process in MvvmCross


Can I customize the navigation process in MvvmCross


Yes, you can customize the navigation process in MvvmCross. Here's a detailed overview of how to achieve this:

Customizing Navigation

MvvmCross uses a ViewModel-first navigation approach, where navigation occurs between ViewModels rather than Views. This allows for platform-independent navigation logic, making it easier to manage complex navigation flows across different platforms like iOS and Android.

Using Custom View Presenters

One way to customize navigation is by using custom View Presenters. MvvmCross provides default View Presenters for each platform, but you can override these to implement custom navigation patterns such as tabbed displays, dialogs, or hamburger menus. To do this, you create a custom presenter class that inherits from the platform-specific presenter (e.g., `MvxFormsPagePresenter` for Xamarin.Forms) and override methods like `CreateNavigationPage` to use your custom navigation components[2][9].

Intercepting Navigation Events

You can also customize navigation by intercepting events provided by the `IMvxNavigationService`. This service offers several events such as `BeforeNavigate`, `AfterNavigate`, `BeforeClose`, and `AfterClose`, which allow you to execute custom logic at different stages of the navigation process. For example, you might use these events to perform additional checks or updates before or after navigating between ViewModels[1].

Uri Navigation and Deep Linking

MvvmCross supports Uri navigation, which allows you to define routes for your ViewModels using assembly attributes. This feature enables deep linking, where specific parts of your app can be accessed directly from external links or notifications. You can define multiple URIs per ViewModel and use "NavigationFacades" to dynamically determine which ViewModel to navigate to based on the URI parameters[1].

Custom Navigation Logic

For more complex scenarios, you can implement custom navigation logic within your ViewModels. For instance, you might use the `CanNavigate` method to check if a navigation is possible before attempting it. Additionally, you can pass parameters between ViewModels using the `ShowViewModel` method with a parameter object, allowing you to transfer data between different parts of your app[1][4].

Custom AppStart

Finally, you can customize the initial navigation of your app by overriding the `Startup` method in a custom `AppStart` class. This allows you to dynamically decide which ViewModel to navigate to based on conditions like user login status or parameters passed from the operating system[4].

Overall, MvvmCross provides a flexible framework for customizing navigation, allowing developers to adapt it to their specific application needs.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/navigation
[2] https://stackoverflow.com/questions/59814748/how-to-use-custom-navigationpage-with-mvvmcross-library-in-xamarin-forms
[3] https://stackoverflow.com/questions/43681201/custom-navigation-controller-in-an-mvvmcross-app
[4] https://www.mvvmcross.com/documentation/tutorials/tipcalc/the-tip-calc-navigation
[5] https://github.com/MvvmCross/MvvmCross/issues/4032
[6] https://softwareengineering.stackexchange.com/questions/120090/who-should-control-navigation-in-an-mvvm-application
[7] https://www.mvvmcross.com/documentation/getting-started/mvvmcross-overview
[8] https://www.mvvmcross.com/documentation/platform/android/android-view-presenter
[9] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup
[10] https://nicksnettravels.builttoroam.com/post-2019-03-24-viewmodel-to-viewmodel-navigation-in-a-xamarinforms-application-with-prism-and-mvvmcross-aspx/