Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I use the new MvxNavigationService in MvvmCross 5.0


How can I use the new MvxNavigationService in MvvmCross 5.0


MvvmCross 5.0 introduces a new navigation system centered around the `IMvxNavigationService`, which significantly enhances how you navigate between ViewModels in your application. Here's a detailed guide on how to use this service:

Overview of MvxNavigationService

The `IMvxNavigationService` is designed to make navigation more flexible and testable. It allows you to inject the service into your ViewModels, enabling you to navigate between them in a type-safe manner. This service supports asynchronous navigation, URL navigation with deeplinking, and events for navigation changes.

Key Features

- Type-Safe Navigation: You can navigate to specific ViewModels using generic types, ensuring that the navigation is strongly typed.
- Async Navigation: Navigation methods are asynchronous, allowing for better handling of asynchronous operations during navigation.
- URL Navigation: Supports URL-based navigation, which is useful for deeplinking directly to specific ViewModels.
- Navigation Events: Provides events like `BeforeNavigate`, `AfterNavigate`, `BeforeClose`, and `AfterClose`, allowing you to intercept and react to navigation changes.
- Return Results: Allows a ViewModel to return results to the ViewModel that initiated the navigation.

Using IMvxNavigationService

To use the `IMvxNavigationService`, you need to inject it into your ViewModel's constructor. Here's an example:

csharp
using MvvmCross.Navigation;

namespace MyNamespace
{
    public class FirstViewModel : MvxViewModel
    {
        private readonly IMvxNavigationService _navigationService;

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

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

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

Checking Navigation Capability

Before navigating, you can check if navigation to a specific ViewModel is possible using the `CanNavigate` method:

csharp
if (Mvx.IoCProvider.Resolve().CanNavigate())
{
    // Proceed with navigation
}

Passing Parameters

If you need to pass parameters to the ViewModel you are navigating to, you can do so by implementing the `IMvxViewModel` interface and overriding the `Init` method:

csharp
public class DetailViewModel : MvxViewModel
{
    protected override Task Init(DetailParameter parameter)
    {
        // Use the parameter here
        return base.Init(parameter);
    }
}

public class DetailParameter
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Then, navigate to `DetailViewModel` with parameters like this:

csharp
var parameter = new DetailParameter { Name = "John", Age = 30 };
_navigationService.Navigate(parameter);

Handling Navigation Events

You can hook into navigation events to perform actions before or after navigation occurs:

csharp
Mvx.IoCProvider.Resolve().BeforeNavigate += (sender, e) =>
{
    // Perform actions before navigating
};

Mvx.IoCProvider.Resolve().AfterNavigate += (sender, e) =>
{
    // Perform actions after navigating
};

This approach allows you to manage navigation centrally and react to changes in your application's state.

Citations:
[1] https://www.mvvmcross.com/mvvmcross-5-release/
[2] https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross/Navigation/IMvxNavigationService.cs
[3] https://stackoverflow.com/questions/45019264/open-view-without-navigation-in-mvvmcross-xamarin-forms-app/45040352
[4] https://www.mvvmcross.com/documentation/tutorials/tipcalc/the-tip-calc-navigation
[5] https://stackoverflow.com/questions/49708064/mvvmcross-passing-a-string-with-imvxnavigationservice
[6] https://www.mvvmcross.com/mvvmcross-51-release/
[7] https://www.mvvmcross.com/documentation/fundamentals/navigation
[8] https://stackoverflow.com/questions/45929520/mvvmcross-pass-a-parameter-to-a-viewmodel
[9] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle