MVVM - what should contain what ... what should create what

I have a right-handed barney who looks back on how everything fits together using the MVVM pattern. All this seems pretty simple in practice, but trying to implement it, I seem to be breaking other rules that I'm trying to follow.

As a remark, I'm trying to implement a template using Flex rather than Silverlight or WPF, so if anyone can come up with good reasons why this should not be done, I'd love to hear them.

I have a problem when I have multiple views. Sometimes I have to simultaneously display two views on a page; sometimes I come back to one look. In my regular Flex brain, I would have a basic view with code that would contain all my other views (equally with code entries). This main view will then switch other individual views.

When I try to implement this in MVVM, I try to adhere to the principles of MVVM using a binding that separates my Viewsfrom ViewModels. Let's say I create an ViewModelapplication for the general state, and mine ApplicationViewcontacts this data and performs all the switching of sub-views.

Now, where should I create my view models for my routines? I tried inside ApplicationView- it seemed wrong. And then I tried outside of the application view and passed it and an instance of it to ApplicationView, and then my helper models tied to it. Am I missing something? None of these methods seem to be suitable for fixing this problem.

Any good books or links explaining this issue would be much appreciated.

Cheers, James

+3
source share
3 answers

, , - ViewModel. , ViewModel. ViewModel ViewModel. , ( , ) ViewModel.

ViewModel :

public class RootViewModel 
{
   ChildViewModelA ChildA { get; set; }
   ChildViewModelB ChildB { get; set; }
}

:

<Grid>
   <ChildViewA DataContext="{Binding ChildA}" />
   <ChildViewB DataContext="{Binding ChildB}" />
</Grid>

, .

ViewModel :

public class RootViewModel 
{
   public List<ViewModel> ChildWorkspaces { get; set; }
   public ViewModel ActiveWorkspace { get; set; }

   public RootViewModel() 
   {
      ChildWorkspaces.Add(ChildViewModelA);
      ChildWorkspaces.Add(ChildViewModelB);
   }
}

:

<Grid>
   <Grid.Resources>
      <DataTemplate DataType="ChildViewModelA">
          <ChildViewA />
      </DataTemplate>
      <DataTemplate DataType="ChildViewModelB">
          <ChildViewB />
      </DataTemplate>
   </Grid.Resources>
   <ContentControl Content="{Binding ActiveWorkspace}" />
</Grid>

, , ActiveWorkspace.

, WPF. : -)

, "ViewModel" . - ViewModel. ViewModel - View Model.

MVVM WPF , ( ). DataTemplateSelector . DataContext #/ActionScript, .

, !

+7

MVVM, Flex, , . , , Presentation Models Flex , , , .

, MVVM Flex, ViewModel Model/ModelLoactor. ModelLoactor , ViewModels. ApplicationViews ViewModel ModelLocator, ViewModels , ModelLocator. , ; , , ModelLocator - ViewModels.

, Mate. ViewModels ApplicationViews. ( , Swiz, ). Mate ApplicationView ViewModel, . , , ViewModels EventMap ( Mate FrontController). , ApplicationViews , EventMaps, ViewModels. ApplicationView ViewModels . , Mate EventMaps, ViewModels. , , Mate , .

, !

+3

I wanted to share the comparison that I wrote from MVVM (Silverlight) and PresentionModel (Flex). It shows how two implementations of the same template are different / compared:

http://houseofbilz.com/archives/2010/12/29/cross-training-in-silverlight-flexmvvm-vs-presentation-model/

0
source

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


All Articles