Event Base WPF Navigation
My Recent problem was: How can I implement minimal decoupling navigation between views?
I thought about using the EventAggregator already built in my system and create some kind of event that represents a navigation request, so I created a NavigationRequestEvent that contains the view model I want to activate and view.
Next question I asked myself was: Who needs to request for navigation?
This is easy navigation is caused 99% of the times by user interaction, views is a good answer, through their view models or some commands (if not DelegateCommands or SmarterStuff).
The problem is that my view models are being created from my IOC Container (not a problem) and I don't want all of my view models to have access to my container cause this is an anti pattern.
Who'll construct the view model and fire the event?
Create a view models factory that will be responsible for creating the view models, he is not the actual factory, just a mediator between the request and the container.
So far we got:
- NavigationRequestEvent that holds the view model to show
- ViewModelsFactory - can create view models
Now I just need someone to listen, this is the advantage of this pattern, anyone can listen and no one is also an option, but nothing will happen of course.
In my case the shell is the default listener and each event would be translated to a view shown at the main region of the ui , there is also an option of a split view, when split view activated the main region unregister from event and secondary view is listening to the event.
We can also expend this behavior and allow user to choose where next views will be opened.
IsNice = True;
Comments