Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I implement navigation in a cross-platform app using MvvmCross


How do I implement navigation in a cross-platform app using MvvmCross


Implementing navigation in a cross-platform app using MvvmCross involves utilizing the framework's built-in navigation features, which are designed to work seamlessly across different platforms such as Xamarin.iOS, Xamarin.Android, and Xamarin.Forms. Here's a detailed guide on how to implement navigation:

Understanding MvvmCross Navigation

MvvmCross uses a ViewModel-first approach to navigation, meaning you navigate from one ViewModel to another, and the framework automatically finds the corresponding View for the target ViewModel. This approach simplifies cross-platform development by eliminating the need for platform-specific navigation code[1][7].

Key Components

1. IMvxNavigationService: This is the core interface for navigation in MvvmCross. It provides methods to navigate between ViewModels and supports passing parameters to the target ViewModel[1][9].

2. MvxViewModel: Your ViewModels should inherit from `MvxViewModel` or `MvxViewModel` if you need to pass parameters during navigation[7].

Implementing Navigation

Basic Navigation

To navigate from one ViewModel to another, you inject `IMvxNavigationService` into your ViewModel and use its `Navigate` method:

csharp
using MvvmCross.Navigation;

public class FirstViewModel : MvxViewModel
{
    private readonly IMvxNavigationService _navigationService;

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

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

Navigation with Parameters

If you need to pass data to the target ViewModel, use the `Navigate` method that accepts a parameter:

csharp
public class FirstViewModel : MvxViewModel
{
    private readonly IMvxNavigationService _navigationService;

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

    public IMvxAsyncCommand NavigateCommand => new MvxAsyncCommand(() =>
    {
        var parameter = new MyParameter { /* Initialize properties */ };
        _navigationService.Navigate(parameter);
    });
}

public class SecondViewModel : MvxViewModel
{
    public override async Task Init(MyParameter parameter)
    {
        // Use the parameter here
    }
}

Advanced Navigation Scenarios

Uri Navigation

MvvmCross supports Uri-based navigation, which allows for deep linking and complex navigation structures. You can define navigation routes using assembly attributes:

csharp
[assembly: MvxNavigation(typeof(ViewModelA), @"mvx://test/\?id=(?[A-Z0-9]{32})$")]

Then, navigate using a Uri:

csharp
await _navigationService.Navigate("mvx://test/?id=" + Guid.NewGuid().ToString("N"));

Closing ViewModels

To return to a previous ViewModel, you can close the current one:

csharp
await _navigationService.Close(this);

Customizing Navigation

MvvmCross allows customization through the use of presenters and adapters. For example, you can intercept `ShowViewModel` calls using a presenter to implement custom navigation logic[2].

Conclusion

MvvmCross provides a robust navigation system that simplifies cross-platform app development by abstracting away platform-specific details. By leveraging `IMvxNavigationService` and ViewModel-first navigation, you can create complex navigation flows with ease.

Citations:
[1] https://www.mvvmcross.com/documentation/tutorials/tipcalc/the-tip-calc-navigation
[2] https://stackoverflow.com/questions/18735497/mvvmcross-navigation-on-screen
[3] https://canbilgin.wordpress.com/2017/02/26/developing-universalcross-platform-apps-with-mvvm-vii/
[4] https://www.mvvmcross.com/documentation/getting-started/getting-started
[5] https://leaware.com/creating-cross-platform-xamarin-applications-using-the-mvvmcross-framework/
[6] https://github.com/VincentH-Net/QuickCross
[7] https://www.mvvmcross.com/documentation/fundamentals/navigation
[8] https://github.com/MvvmCross/MvvmCross-Samples
[9] https://yanxiaodi.gitbook.io/xamarin-mvvmcross-handbook/using-mvvmcross-to-create-your-first-xamarin-app/navigation/understanding-the-imvxnavigationservice
[10] https://stackoverflow.com/questions/51474717/mvvmcross-navigation-through-multiple-view-models