Managing navigation with the MVVM pattern
We explored navigation in Chapter 5, Designing Your Application, and we have learned that it's a critical component of a Windows application. Especially if you're building enterprise applications, it's very likely that you will have multiple pages and the various interactions will lead the user to move from one to another. However, it's also one of the most challenging features to implement when you start adopting the MVVM pattern. The reason is that the goal of the pattern is to decouple the UI from the business logic; however, this creates a fracture in this case. The actions performed by the user are managed by the ViewModel, while navigation is managed in the View layer by using the Frame
class to trigger the navigation and by subscribing to events such as OnNavigatedTo()
and OnNavigateFrom()
to manage the life cycle of a page.
There are multiple solutions to this challenge, but the most common one is based on the...