Search code examples
c#androidmvvmmvvmcross

mvvm - navigate to same View from multiple ViewModels in mvvmcross


I'm currently using fragments with 1-View-to-1-ViewModel mapping. However, I have a special case where a View A (as fragment) is bound to ViewModel A, with a ViewModel B that extends ViewModel A. Instead of having a View B for ViewModel B, is it possible to use View A for ViewModel B?

As I'm doing navigation through ViewModels, both ViewModel B or ViewModel A should lead to View A.

Any suggestions on how this can be achieved?

Since I'm still new to this I have only done some research. I don't if it would make sense to use 1-View-to-2-ViewModels mapping? If so, I can see that MvxFragment only supports one generic ViewModel parameter, therefore I don't know if this can be only achieved by own implementation?

UPDATE: I found some answers and I'm about to test them out:

Register a single View as the View for Multiple ViewModels - MVVMCross

How can i use one view for different view models in mvvmcross?


Solution

  • I solved the problem with the folowing approach by making another ViewA that can be extended by other Views:

        class ViewA<TViewModel> : BaseView<TViewModel> where TViewModel : ViewModelA
        {
          // view init and logic
        }
        
        class ViewA : ViewA<ViewModelA> { }
        
        class ViewB : ViewA<ViewModelB> { }