When a user has entered a correct password, a login window should close and the main window should open. I would like to do this from the ViewModel, however, the ViewModel may not know anything about the view.
I’ve searched a lot and apparantly the best way is to use the mediator pattern. I understand how this pattern works, but how to effectivly use it in this case?
the mediator will need:
– A reference to the View
– A reference to the ViewModel
The ViewModel will need a reference to the mediator.
Where do I need to create the mediator? I can’t do it in the ViewModel as I won’t be able to set the reference to the view. Creating the mediator in the view is an option as I can get the ViewModel out of the DataContext property, but then I will still need to pass the mediator to the ViewModel, which will only make the code more difficult.
How do I properly use a mediator for opening/closing windows?
the mediator pattern is used for communication between viewmodels not between view and viewmodel.
if you wanna handle dialogs from your viewmodel you can use a dialogservice like this one.
nevertheless when i wanna create a application login dialog, i do it this way.
in app.xaml.cs OnStartup()
here some of my code
ps: this is the only part of my apps where i use view first. the rest is all viewmodel first, which is much easier for me when doing mvvm.