Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I pass parameters between ViewModels in MvvmCross


How can I pass parameters between ViewModels in MvvmCross


Passing parameters between ViewModels in MvvmCross is a common requirement for navigating between different parts of an application while maintaining a robust MVVM architecture. Here's how you can achieve this:

Using `ShowViewModel` with Parameters

MvvmCross provides a method called `ShowViewModel` that allows you to navigate to another ViewModel. You can pass parameters to the target ViewModel by using the generic version of this method:

csharp
ShowViewModel(new TParameter());

Here, `TViewModel` is the type of the ViewModel you want to navigate to, and `TParameter` is the type of the parameter you want to pass.

Implementing `MvxViewModel`

For the receiving ViewModel to accept these parameters, it should implement `MvxViewModel`. This involves overriding the `Init` method to handle the parameter:

csharp
public class MyViewModel : MvxViewModel
{
    protected override Task Init(MyParameter parameter)
    {
        // Use the parameter here
        return Task.CompletedTask;
    }
}

Using `IMvxViewModel` Interface

If your ViewModel already inherits from another base class (e.g., a custom `BaseViewModel`), you can't directly inherit from `MvxViewModel`. In this case, you can implement the `IMvxViewModel` interface instead:

csharp
public class MyViewModel : BaseViewModel, IMvxViewModel
{
    public Task Init(MyParameter parameter)
    {
        // Use the parameter here
        return Task.CompletedTask;
    }
}

JSON Serialization

To pass complex parameters, ensure that MvvmCross can serialize them. This typically involves installing the MvvmCross Json plugin or registering a custom `IMvxJsonConverter` for your parameter types.

Example Usage

Here's a complete example of how to pass a parameter from one ViewModel to another:

1. Parent ViewModel:

csharp
   public class ParentViewModel : MvxViewModel
   {
       public ICommand ShowChildCommand { get; private set; }

       public ParentViewModel()
       {
           ShowChildCommand = new MvxCommand(() => ShowViewModel(new MyParameter()));
       }
   }
   

2. Child ViewModel:

csharp
   public class ChildViewModel : MvxViewModel
   {
       protected override Task Init(MyParameter parameter)
       {
           // Handle the parameter here
           return Task.CompletedTask;
       }
   }
   

3. MyParameter Class:

csharp
   public class MyParameter
   {
       public string SomeData { get; set; }
       // Other properties...
   }
   

By following these steps, you can effectively pass parameters between ViewModels in MvvmCross, maintaining a clean and scalable architecture.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/navigation
[2] https://docs.devexpress.com/WPF/17448/mvvm-framework/viewmodels/passing-data-between-viewmodels-isupportparameter
[3] https://www.mvvmcross.com/documentation/fundamentals/view-presenters
[4] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[5] https://www.reddit.com/r/androiddev/comments/17alr8x/are_there_actually_any_guidelines_on_how_to_pass/
[6] https://github.com/MvvmCross/MvvmCross/issues/215
[7] https://stackoverflow.com/questions/45929520/mvvmcross-pass-a-parameter-to-a-viewmodel
[8] https://stackoverflow.com/questions/59725322/xamarin-mvvm-pass-data-to-other-viewmodels-and-shared-viewmodel
[9] https://learn.microsoft.com/en-us/answers/questions/1410617/passing-data-between-2-viewmodels-in-wpf-mvvm