A
A
artemvalieiev2014-09-15 12:19:06
.NET
artemvalieiev, 2014-09-15 12:19:06

How to save the state of the ViewModel after navigating to the same page with different parameters for the ViewModel?

For example, I have a view that describes the current room in the viewModel , from the room I can go to another room with the same viewModel , where I will pass the parameters of the new room, but if I press back the NavigationService , the view room will go back, and the viewModel will save the state of the new rooms, i.e. Viw update will not happen to the previous one.
How to solve this problem? Save a stack viewmodel for each room id for example and use it during navigation? I understand this is a typical situation, similar to redo,undo (Command pattern)? Or is Memento even more suitable?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aush, 2014-09-15
@artemvalieiev

You do not need to save previous states and transfer them between views. A good architecture is this: you have a repository that contains data about rooms, you can uniquely identify rooms. When you want to render a view for a room with a given ID, you query the repository for the domain model of the room, map it to the viewmodel, and use that viewmodel to render the view. That. you don't have the concept of "move from one room view to another", you just have "view view for that room".

A
artemvalieiev, 2014-09-15
@artemvalieiev

I found that a ViewModel is created in the IoC container with a key or not created at all, during navigation the parameter is passed through the Frame and not through the Messenger class, the view saves the key and receives the ViewModel for a specific key from the IoC container, or loads information from the repository, into WPF or Windows Runtime or Silverlight, Frame can cache the view , in case of back navigation, the view gets the viewModel by a specific key, or gets information from the repository for the key.
Read more here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question