Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the steps to implement IMvxResultAwaitingViewModel


What are the steps to implement IMvxResultAwaitingViewModel


Implementing `IMvxResultAwaitingViewModel` in MvvmCross involves creating a ViewModel that can await results from another ViewModel. This is useful when you need to return data from one ViewModel to another after navigation. Here are the detailed steps to implement this interface:

Step 1: Understand the Role of `IMvxResultAwaitingViewModel`

`IMvxResultAwaitingViewModel` is part of a contract that allows a ViewModel to wait for a result from another ViewModel. This is typically used in scenarios where you need to pass data back after navigating to another screen.

Step 2: Implement the Interface

To implement `IMvxResultAwaitingViewModel`, your ViewModel must inherit from this interface and implement the necessary methods. However, in many cases, you will inherit from `MvxResultAwaitingViewModel` instead, which provides a basic implementation.

csharp
public class MyViewModel : MvxResultAwaitingViewModel
{
    private readonly IMvxNavigationService _navigationService;
    private readonly IMvxResultViewModelManager _resultViewModelManager;

    public MyViewModel(IMvxNavigationService navigationService, IMvxResultViewModelManager resultViewModelManager)
        : base(resultViewModelManager)
    {
        _navigationService = navigationService;
        _resultViewModelManager = resultViewModelManager;
    }

    // Other methods and properties...
}

Step 3: Override `ResultSet` Method

The `ResultSet` method is where you handle the result returned from the other ViewModel. This method is called when a result is set by the ViewModel you navigated to.

csharp
public override bool ResultSet(IMvxResultSettingViewModel viewModel, MyReturnObject result)
{
    // Handle the result here
    if (viewModel is NextViewModel)
    {
        // Do something with the result
        Debug.WriteLine(result);
        // Return true to mark it handled and unregister
        return true;
    }
    return false;
}

Step 4: Use `NavigateRegisteringToResult` for Navigation

To navigate to another ViewModel and await a result, use the `NavigateRegisteringToResult` extension method provided by MvvmCross. This method registers your ViewModel to receive the result from the target ViewModel.

csharp
public async Task SomeMethod()
{
    await _navigationService.NavigateRegisteringToResult(new MyObject(), _resultViewModelManager);
}

Step 5: Implement `IMvxResultSettingViewModel` in the Target ViewModel

The target ViewModel (e.g., `NextViewModel`) should implement `IMvxResultSettingViewModel` to set the result. This ViewModel should also inherit from `MvxResultSettingViewModel`.

csharp
public class NextViewModel : MvxResultSettingViewModel
{
    private readonly IMvxNavigationService _navigationService;
    private readonly IMvxResultViewModelManager _resultViewModelManager;

    public NextViewModel(IMvxNavigationService navigationService, IMvxResultViewModelManager resultViewModelManager)
        : base(resultViewModelManager)
    {
        _navigationService = navigationService;
        _resultViewModelManager = resultViewModelManager;
    }

    public async Task SomeMethodToClose()
    {
        await _navigationService.CloseSettingResult(this, new MyReturnObject(), _resultViewModelManager);
    }
}

Step 6: Ensure Proper Registration and Unregistration

Make sure to properly register and unregister your ViewModel to receive results. This is typically handled by the `MvxResultAwaitingViewModel` base class, but you may need to manually manage this in certain scenarios.

By following these steps, you can effectively implement `IMvxResultAwaitingViewModel` to handle navigation and result passing between ViewModels in MvvmCross.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/navigation
[2] https://learn.microsoft.com/en-us/dotnet/architecture/maui/mvvm-community-toolkit-features
[3] https://developer.android.com/topic/libraries/architecture/viewmodel
[4] https://www.infoq.com/articles/View-Model-Definition/
[5] https://wiki.mdriven.net/Documentation:ViewModel
[6] https://builtin.com/software-engineering-perspectives/mvvm-architecture
[7] https://wiki.mdriven.net/Documentation:ViewModel_actions
[8] https://www.freecodecamp.org/news/the-model-view-controller-pattern-mvc-architecture-and-frameworks-explained/