WPF + MvvM + Prism

后端 未结 2 2004
故里飘歌
故里飘歌 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:10

    What you got there makes sense and in both cases is a View-first approach to creating a viewmodel. I.e. the view creates the ViewModel. In the original example the viewmodel is created outside of the view (and is sometimes referred to as marriage pattern), but as far as I am concerned that's the same thing - creation of the view creates the ViewModel.

    If this suits your needs stick with it. Another approach you might look into is ViewModel first where the viewmodel takes a dependency on the view like so:

    //In the bare-bones(i.e. no WPF dependencies) common interface assembly
    
    interfac IView {
      void ApplyViewModel(object viewmodel);
    }    
    
    interface IMainView : IView {
      //this interface can actually be empty.
      //It's only used to map to implementation.
    }
    
    //In the ViewModel assembly
    
    class MainViewModel {
      public MainViewModel(IMainView view) {
        view.ApplyViewModel(this);
      }
    }
    
    public partial class MainView : UserControl, IMainView {
      void ApplyViewModel(object viewmodel){
        DataContext = viewmodel;
      }
    }
    

    Then you can inject this view like so:

    IRegion region = regionManager.Regions["MainRegion"];
    
    //This might look strange as we are resolving the class to itself, not an interface to the class
    //This is OK, we want to take advantage of the DI container 
    //to resolve the viewmodel's dependencies for us,
    //not just to resolve an interface to the class.
    MainViewModel mainViewModel = container.Resolve();
    
    region.Add(mainViewModel.View, "MainView");
    region.Activate(ordersView.View);
    

提交回复
热议问题