Search code examples
c#xamarin.formsfreshmvvm

How to set same viewmodel to two views in fresh mvvm in xamarin forms


I have two views Add and Edit view both views are different UI but same viewmodel. By using mvvm i have set binding context to both view as same viewmodel. I am new to fresh mvvm. In fresh mvvm, I don't know how to set same viewmodel to two views. Another one is, I have passing parameter to the viewmodel constructor, how it is possible in fresh mvvm. Please give your valuable suggestion.

Exception thrown in root level page


Solution

  • Normally, the accompanying view would be inferred by the names. But for this use case, you should explicitly mention which view to use. There is a method for that, you should be able to use it like this:

    // For adding
    CoreMethods.PushPageModel<AddEditViewModel, AddView>();
    
    // For editing
    CoreMethods.PushPageModel<AddEditViewModel, EditView>();
    

    Of course, taking into account the actual parameters that you still need to specify for the PushPageModel method. Also, I have assumed the names of your view model and views here, but I hope it gets the point across.