WPF + MvvM + Prism

后端 未结 2 2033
故里飘歌
故里飘歌 2021-02-06 16:31

I am new in the Wpf & Mvvm world , but I have found a couple of examples and just found that there is some different way to instantiate the model. I would like to know the b

2条回答
  •  不要未来只要你来
    2021-02-06 17:18

    I like Igor's suggestion, but without the viewmodel having knowledge of the view. I prefer my dependencies to go one direction (View -> ViewModel -> Model).

    What I do is ViewModel-First and just DataTemplate the viewmodel. So I do this:

    MainViewModel mainViewModel = container.Resolve();
    
    region.Add(mainViewModel, "MainView");
    region.Activate(mainViewModel);
    

    With the addition of the ViewModel -> View mapping done with a WPF datatemplate (I don't think this approach is possible with Silverlight, though)

    App.xaml:

    
         
              
         
    
    

    That's it! I love this approach. I like the way it feels like magic. It also has the following advantages:

    • Don't have to modify constructors to suit the mapping
    • Don't have to register type for IMyViewModel in the container... you can work with concrete types. I like to keep my registrations to application services like IViewRegistry or ILogger... those kinds of things
    • You can change the mapping using resources scoped to a particular view that a region is in (this is nice if you want to reuse your ViewModels but want them to look different in different areas of the application

提交回复
热议问题