WPF + MvvM + Prism

I am new to the world of Wpf and Mvvm, but I found some examples and just found that there are several ways to instantiate a model. I would like to know the best / right way to do this. both methods use Unity

What am I foud:

var navigatorView = new MainView(); navigatorView.DataContext = m_Container.Resolve<INavigatorViewModel>(); m_RegionManager.Regions["NavigatorRegion"].Add(navigatorView); 

What I've done:

 var navigatorView = m_Container.Resolve<MainView>; m_RegionManager.Regions["NavigatorRegion"].Add(navigatorView); 

and I changed the constructor to get the viewmodel so that I can point the datacontext to it:

 public MainView(NavigatorViewModel navigatorViewModel) { this.DataContext = navigatorViewModel; } 

Other examples I found another way:

 ...vm = new viewmodel ...m = new model v.model = vm; 

get / set DataContext

amuses

+4
source share
2 answers

I like Igorโ€™s suggestion, but without a viewmodel that knows the point of view. I prefer my dependencies to go in one direction (View -> ViewModel -> Model).

What I am doing is ViewModel-First and just a DataTemplate in view mode. So I do this:

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

With the addition of the ViewModel -> View view made using the WPF dataset (I don't think this approach is possible with Silverlight, though)

App.xaml:

 <Application.Resources> <DataTemplate DataType="{x:Type viewModels:MainViewModel}"> <views:MainView /> </DataTemplate> </Application.Resources> 

What is it! I like this approach. I love the way it looks like magic. It also has the following advantages:

  • No need to change the constructors to match the mapping
  • No need to register a type for IMyViewModel in a container ... you can work with specific types. I like to register with services like IViewRegistry or ILogger ... things like that
  • You can change the display using resources tied to a specific view in which the region is located (this is good if you want to reuse ViewModels, but want them to look different in different areas of the application.
+8
source

What you have makes sense, and in both cases it is a View-first approach to creating a viewmodel. That is, the view creates a ViewModel. In the original example, the viewmodel is created outside of the view (and is sometimes referred to as a marriage template ), but as far as I know the same thing - creating the view creates a ViewModel.

If it suits your needs, stick to it. Another approach you can explore is ViewModel, when the view model has a view dependency:

 //In the bare-bones(ie no WPF dependencies) common interface assembly interfac IView { void ApplyViewModel(object viewmodel); } interface IMainView : IView { //this interface can actually be empty. //It 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 enter this view like this:

 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 dependencies for us, //not just to resolve an interface to the class. MainViewModel mainViewModel = container.Resolve<MainViewModel>(); region.Add(mainViewModel.View, "MainView"); region.Activate(ordersView.View); 
0
source

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


All Articles