MVVM View-First Approach How to Change the View

Does anyone have an idea how to change screens (views) in MVVM View-First-Approach (the view creates an instance of ViewModel:

DataContext="{Binding Source={StaticResource VMLocator}, Path=Find[EntranceViewModel]}" 

)

For example: In my MainWindow (Shell), I show the input view with the "GoToBeach" button.

 <Window> <DockPanel> <TextBox DockPanel.Dock="Top" Text="{Binding Title}" /> <view.EntranceView DockPanel.Dock="Top" /> </DockPanel> </Window> 

When the button is pressed, I want to get rid of "EntranceView" and show "BeachView". I'm really curious if someone knows a way to keep the “First Look” approach and change the screen (view) to “BeachView”. I know that there are several ways to implement it in the first approach to ViewModel, but this is not a question. Perkhubs, I missed something in my mvvm study and don't see a tree for trees ... otherwise I'm hoping for an inspiring discussion.

+4
source share
3 answers

One of the possibilities is to have all views in (MainWindow (Shell) and use Triggers to make them visible. But the presence of a large number of different screens (views) declared in MainWindow does not seem right to me ...

This question came up while reading this good way to use MEF with MVVM, which I found on John Papas's blog: a simple ViewModel locator for MVVM: patients left the shelter. But as nice as this marriage of view and viewmodel, there seems to be no way to change the screens that satisfy me. :)

So, in my opinion, if you have many screens (views), you better use ViewModel-First-Approach ...

+1
source

It seems like this might help: Creating a ViewModel: is it available before or after the model data is available?

Otherwise, how about creating a ViewModel once only at startup and assigning it to each View window when creating it (instead of creating a new ViewModel every time). Then simply close the first view and open a new View, if necessary, by reassigning a single instance of the ViewModel.

0
source

You can look at Prism (i.e. a composite application library). Prism facilitates navigation between views through the region manager. This may be redundant for your application and may take some time to plunge. Prism also allows you to develop using the MVVM pattern.

You can find more information on prismatic and prismatic navigation in the documentation.

0
source

Source: https://habr.com/ru/post/1307694/


All Articles