Home c# The principle, structure and use of WPF framework mvvmlight

The principle, structure and use of WPF framework mvvmlight




I’m trying to deal with the MVVM Light . And more and more terrible. Questions arise as a snowball. The network did not find any articles that would be a simple Russian language was due to mvvm-light and its practical application. Below I will give places and moments that cause misunderstanding me.

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


The file App.xaml see the following & lt; Application ... StartupUri = "MainWindow.xaml" ... & gt; . As was explained here, it is not suitable for a number of reasons, and therefore it is necessary to remove this attribute and overloaded methods OnStartup class App to create a copy of the required presentation model and set it as 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 generated project mvvm-light class MainViewModel constructor takes in the IDataService dataService . The project did not find how to create the dataService . Whether the view model for each need the dataService to create? How, when and where it is used?

Also present in the generated project class ViewModelLocator . The commentary Class Set an example using:

/ *
In App.xaml:
& lt; application.resources & gt;
  & Lt; vm: ViewModelLocatorTemplate xmlns: vm = "clr-namespace: MvvmProject.ViewModel"
                x: Key = "Locator" / & gt;
& lt; /pplication.resources>
In the View:
DataContext = "{Binding Source = {StaticResource Locator}, Path = ViewModelName}"
* /

, but it seems like a clear indication DataContext in XAML wrong ?! Or am I wrong ?! Why and how is it used?

In the code-behind submission MainWindow Event Closing adds cleaning ViewModelLocator :

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

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

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

& lt; Window.Resources & gt;
  & lt; ResourceDictionary & GT;
    & lt; resourcedictionary.mergeddictionaries & gt;
      & Lt; ResourceDictionary Source = "Skins / MainSkin.xaml" / & gt;
    & lt; /resourcedictionary.mergeddictionaries>
  & lt; / ResourceDictionary & gt;
& lt; /window.resources>

As I understand it, it is only used for working with the visual editor and the working version does not matter. Correct me if I’m wrong.

There are many examples of MVVM-LIGHT on the network, but they all go down to the blind consequences of the instructions, without explanation for which it is done and how it is later used. Anticipating the reproaches for the study of the Mat.Ships, I will note that I personally understand the material easier in practice. And I would not want to beat myself the cones, and take the experience of knowledgeable people and immediately learn as need to be done, and not as you can . There may be any project on the network, which describes in detail the use of MVVM-Light framework in Russian ?

Answer 1, Authority 100%

The simplest is to follow the recommendations, and do everything through ViewModellocator . Dataservice is created through the injection from the container.

So registration occurs in viewmodellocator.cs
In the designer we write

simpleioc.default.register & lt; IdataTask, DebugDataContext & gt; ();

Where DebugdataConxtext is a specific implementation of the IDATATASK for example, a database for debugs, and in the production will be taken from another place (but this is a completely different story).

Register VM

simpleioc.default.register & lt; mainviewmodel & gt; ();

Exhibit it to the public

Public MainViewModel Main = & gt; Servicelocator.current.getInstance & lt; MainViewModel & gt; ();

All this is already the default, for other forms, simply by analogy.
And when the look will turn to VM through that code in XAML automatically via IOC will be cling to the desired data source. You answered there that the look should not create VM. But she does not create, creates an Ioc-container, a look only requests its copy. So you did not quite rightly answer, without injections, everything is true, but in MVVM Light already have Ioc and the entire paradigm is built around injection

In the code, you can also, if I really want, but I personally do not see in this sense

mainvm = servicelocator.current.getInstance & lt; mainviewmodel & gt; ();

Of course, all this IMHO, Vlad’a has its views, I have my own, and MVVM is not holy writing, but just an idea and use it in different ways, but on the idea of ​​the creators of MVVM Light it should be done about since I described.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions