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 ?