Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you explain how to implement IMvxViewModel<TParameter> in MvvmCross


Can you explain how to implement IMvxViewModel<TParameter> in MvvmCross


Implementing `IMvxViewModel` in MvvmCross allows you to pass parameters between view models during navigation. Here's a step-by-step guide on how to implement it:

1. Define Your Parameter Type

First, you need to define the type of parameter you want to pass. This could be a simple type like `string` or a complex object.

csharp
public class MyParameter
{
    public string Name { get; set; }
    public int Age { get; set; }
}

2. Implement `IMvxViewModel` in Your ViewModel

Your view model should inherit from `MvxViewModel` and implement `IMvxViewModel`. However, if you already have a base view model, you might not be able to directly inherit from `MvxViewModel`. In such cases, you can implement the `IMvxViewModel` interface directly.

csharp
public class MyViewModel : MvxViewModel, IMvxViewModel
{
    public void Prepare(MyParameter parameter)
    {
        // Save or use the parameter here
        var name = parameter.Name;
        var age = parameter.Age;
    }
}

Alternatively, if you can inherit from `MvxViewModel`, it looks like this:

csharp
public class MyViewModel : MvxViewModel
{
    public override void Prepare(MyParameter parameter)
    {
        // Save or use the parameter here
        var name = parameter.Name;
        var age = parameter.Age;
    }
}

3. Navigate with Parameters

To navigate to `MyViewModel` with parameters, you can use the `ShowViewModel` method provided by MvvmCross. This method is typically accessed through the `IMvxNavigationService`, but you can also use it directly in your view models.

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

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

    public async Task NavigateToMyViewModel()
    {
        await _navigationService.Navigate(new MyParameter { Name = "John", Age = 30 });
    }
}

Alternatively, without using `IMvxNavigationService` explicitly, you can simply call `ShowViewModel`:

csharp
public class SourceViewModel : MvxViewModel
{
    public async Task NavigateToMyViewModel()
    {
        await ShowViewModel(new MyParameter { Name = "John", Age = 30 });
    }
}

4. Handling Complex Parameters

If you are passing complex objects as parameters, ensure that MvvmCross can serialize them. You might need to install the MvvmCross Json plugin or register your own `IMvxJsonConverter` for custom serialization.

5. Base ViewModel Scenario

If you want to create a base view model that supports parameter passing, you can implement `IMvxViewModel` in your base class. However, this requires careful handling if some of your view models do not need parameters.

csharp
public abstract class BaseViewModel : MvxViewModel, IMvxViewModel
{
    public abstract void Prepare(TParameter parameter);
}

public class MyViewModel : BaseViewModel
{
    public override void Prepare(MyParameter parameter)
    {
        // Use the parameter
    }
}

For view models that don't need parameters, you might use `object` as the parameter type and implement an empty `Prepare` method:

csharp
public class ParameterlessViewModel : BaseViewModel
{
    public override void Prepare(object parameter)
    {
        // No action needed
    }
}

This approach allows you to enforce parameter handling in view models that require it while still providing flexibility for those that do not.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/navigation
[2] https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross/Navigation/IMvxNavigationService.cs
[3] https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/august/windows-phone-build-mvvm-apps-with-xamarin-and-mvvmcross
[4] https://stackoverflow.com/questions/45355003/trouble-creating-a-base-viewmodel-for-mvvmcross-5-1-0
[5] https://github.com/MvvmCross/MvvmCross/issues/1634
[6] https://stackoverflow.com/questions/32067695/mvvmcross-missing-viewmodels-parameters-with-link-all-assemblies-enabled?rq=1
[7] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[8] https://leaware.com/creating-cross-platform-xamarin-applications-using-the-mvvmcross-framework/
[9] https://yanxiaodi.gitbook.io/xamarin-mvvmcross-handbook/using-mvvmcross-to-create-your-first-xamarin-app/navigation/understanding-the-imvxnavigationservice