Trying to deal with MVVM Light . And the further, the worse. Questions arise like a snowball. I have not found a single article on the web where mvvm-light and its practical application could be explained by simple Russian. Below I will give the places and moments that cause me to misunderstand.

Installed on VisualStudio 2015 MVVM Light, created a new project MvvmLight (WPF451). The project created the following files and folders:

[Design] DesignDataService.cs [Model] DataItem.cs DataService.cs IDataService.cs [Skins] MainSkin.xaml [ViewModel] MainViewModel.cs ViewModelLocator.cs App.xaml App.xaml.cs MainWindow.xaml MainWindow.xaml.cs 

In the App.xaml file, App.xaml see the following <Application ... StartupUri="MainWindow.xaml" ...> . As it was found out here , this is not suitable for a number of reasons, and therefore you need to remove this attribute and, in the App class class overloaded OnStartup method, create an instance of the required view model and set it as a DataContext , like this:

 public partial class App : Application { MainViewModel mainVM = new MainViewModel(); static App() { DispatcherHelper.Initialize(); } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); new MainWindow() { DataContext = mainVM }.Show(); } } 

, but when used in the created mvvm-light project, the MainViewModel class accepts MainViewModel in the IDataService dataService . In the project I did not find how this dataService is created. Does each dataService model need to create this dataService ? How, when and where is it used?


Also in the created project there is a class ViewModelLocator . The class comment has an example of use:

 /* In App.xaml: <Application.Resources> <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:MvvmProject.ViewModel" x:Key="Locator" /> </Application.Resources> In the View: DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}" */ 

, but it seems the same as explicitly specifying the DataContext in XAML is wrong ?! Or am I wrong? What is it used for and how?

In the code-behind of the MainWindow view, the ViewModelLocator added to the Closing event:

 Closing += (s, e) => ViewModelLocator.Cleanup(); 

, but nothing is added to the ViewModelLocator . Or does it happen somewhere far behind the scenes ?!


In the MainWindow.xaml file there is a binding to the resource:

 <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Skins/MainSkin.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> 

As I understand it, this is used only for working with the visual editor and does not play any role in the working version. Correct me if I'm wrong.


There are many examples of mvvm-light on the network, but they all boil down to blindly following instructions, without explaining why this is done and how it is subsequently used. Anticipating the reproaches about the study of the material, I will note that I personally personally learn the material more easily in practice. And I would not want to bang myself bumps, and learn from the experience of knowledgeable people and immediately learn how to do it, not how you can . Maybe there is a project in the network, which describes in detail the use of the mvvm-light framework in Russian ?

  • So take another learning framework. - vitidev
  • @vitidev for example ?! What kind of framework is the documentation in Russian ?! - XelaNimed
  • It is not necessary to use MVMM Light, you can create all the necessary tools yourself, or pull it from the same MVVM Light - Gardes
  • one
    @ S.Kost I agree with you, but as I wrote above I would like to , as needed , and not as much as possible . My qualifications will not allow you to create a good bike yourself. And if you pull from MVVM Light, then we return to my question: "How to use it?" - XelaNimed

1 answer 1

The simplest thing is to follow the recommendations, and do everything through the ViewModelLocator . DataService is created through injections from an IoC container.

So registration takes place in ViewModelLocator.cs
in the constructor we write

 SimpleIoc.Default.Register<IDataTask, DebugDataContext>(); 

where DebugDataContext is a specific implementation of the IDataTask interface, IDataTask as a database for debug, and in production it will be taken from another place (but this is a completely different story).

Registering VM

 SimpleIoc.Default.Register<MainViewModel>(); 

Expose it to the public

 public MainViewModel Main => ServiceLocator.Current.GetInstance<MainViewModel>(); 

All this is already the default, for other forms simply by analogy. And when the viewer accesses the VM through that code in XAML, the necessary data source will be automatically IoC through IoC . You were told there that the view should not create a VM. but it does not create, creates an IoC container, the view only asks for its copy. So they didn’t answer you right there, without injections, everything is really the case, but MVVM Light already has IoC and the whole paradigm is built around injections

In the code you can, too, if you really want, but personally I do not see the point

mainVM = ServiceLocator.Current.GetInstance<MainViewModel>();

Of course, all this is IMHO, VlaD has its own views, I have my own, and MVVM is not a holy scripture, but just an idea and use it in different ways, but as conceived by the creators of MVVM Light, this should be done approximately as I described.