The Struggle for Understanding MVVM Architecture

I am trying to learn MVVM and WPF and I am using MVVM Light Toolkit. Here I do not quite understand and, possibly, due to the incorrect architecture of my interface.

What I'm trying to do is pretty simple. By the way, this is a utility application. I want a window that serves as a "controller", so to speak, with a set of buttons. Each button should change the contents of the frame. Example: one button loads the “screen” (or “view” if you do), which allows the user to configure the “Agency”, which is a custom object. Another button loads the list of users from the Agency, which was on the first "screen". This Users view must also be loaded into the same frame. In fact, at the moment the window with all the buttons is really only responsible for loading the “screens” in the frame. The application ball will be in all separate "screens"

What I do not understand is 1) how each screen / view knows about each other, since it depends on the other. It seems that MVVM ViewModel should not know anything. But in my case, I need to transfer information (for example, my agency).

If I can get some clues about what I need to learn, this will be great.

Thanks!

+7
source share
4 answers

Some ideas that may connect some points:

  • Most likely, you will have one view model (“screen”).
  • Each view model will contain all the logic for the corresponding view.
  • Viewmodels can and will know about models (Agency, Users)
  • Viewmodels can communicate with each other via Messenger in MVVM Light mode
  • Think of MVVM Light Messenger as a “common event system." When you send a message from one view model, any other view model can listen to this message / event and respond to it as necessary.

Does it help at all? Keep your thoughts and I will continue to comment, and I am sure that the community will also be :)

+13
source

A few things:

  • each of your screens should have a separate view (for example, a user control or a new window - suppose you already did this)
  • each part of the model (for example, agency, user) that you want to display in the application must be wrapped with a special viewing model
  • Your views do not have to know about each other; you can use commands or events in view models to get rid of these dependencies.
  • Only a view model should know only one thing : model it on
  • It's good to think of a presentation as a really simple class, with one responsibility for providing the content; no logic, no code behind (unless it is connected to the UI / display), then you should follow

First you can try to prepare your models (if you have not already done so), and then create view models for them (think about what properties of the models you want to display for views), and as soon as this is ready, build your views based on models viewing. Another way is also a viable option - to choose what seems more natural to you.

One more thing: since you mentioned that you can display multiple screens in one (I suppose) main area, consider equipping your bool IsCurrentlyActive with something in accordance with the bool IsCurrentlyActive property. Thus, you can easily show / hide views with the click of a button and use the snap mechanism.

+4
source

They should not know about each other. This is what Messenger for controllers and views subscribes to events of interest to them. Thus, they do not need to know and not care about where they come from.

Hmm, Kendrick is faster. What did he say.

It also sounds like you want an interface like Outlook, as well as some kind of navigation that loads other views. Some time ago I had the same question. How to make regions in WPF without a prism?

+2
source

To better understand the MVVM pattern, take a look at this article: WPF Applications with Model-View-ViewModel Design Pattern

I also advise you to take a look at the Caliburn Micro circuit .

+1
source

All Articles