WPF - MVVM Screen Management

Imagine you have a complex data object. It was difficult enough that for editing various properties of an object it would be better if the user had several screens. This is essentially a basket for customized items.

Thus, one screen will allow you to add items. Another way will allow you to add changes to these elements, predefined changes related to cost. The third screen allows you to configure global settings for your items.

As I’m sure, you can guess that each screen works on the same cart, simply changing the various properties and relations of the elements inside.

So, we will try to write an application using MVVM, and, discussing the various screens (as well as the navigation between them), we came to the following question:

How do people typically manage application state when using MVVM? The navigation bar that users will use to change screens will exist off-screen, but when a user clicks on it, what are some common ways people use it to hide it and show another?

More generally, how do people handle the state of a global application? The user can only work on one cart at a time, there can only be one user at a time, only one screen can be displayed at a time. Would it be better to create a singleton that retains these important properties, and ViewModels could save a copy of them and subscribe to the changes through the event aggregator?

As you can say, I don’t even know where to start this problem, so any advice is generally welcomed and obtained.

+2
wpf mvvm
source share
2 answers

I would use ViewModels to track application status.

One ViewModel manages the entire application and processes the page on which the user is currently logged on. The application itself is tied to the main ViewModel, and most of the application screen space is ContentControl tied to ViewModel.CurrentPage. DataTemplates are then used to determine which view to display for any page that the user is currently on.

In the past, I used a global singleton for some objects (for example, the current user), and ViewModels use a link to this if necessary. Therefore, if I wanted to display UserName on the page, I would have a property in ViewModel named UserName and it returns Global.Instance.CurrentUser.UserName

+3
source share

For your type of situation, I would consider PRISM . PRISM is a set of templates for developing WPF applications using loosely coupled MVVM.

PRISM Region Manager Example

In particular, for your example of multiple screens and managing the state of an application, using the “Controller” to load views for various views of your ViewModel (basket) into separate “regions” is likely to be a good start. MSDN has an article about getting started with PRISM , including compiling user interfaces (regions).

+1
source share

All Articles