MVVM knowledge, mvvm framework
I have learned about WPF for two years, but I have also worked on several projects. During the previous interview, the interviewer must ask about MVVM.
In fact, I don't know much about it. I just used this mode when I was working on a project, Model-View-ViewModel. Here are some excerpts from my understanding:
1) View is mainly used for interface presentation and interaction with user input devices. In code-Behind, you can also use some UI logic, such as rich animation effects, alternatively, you can directly set a metadata style. In addition, the logic for setting the DataContext of the View layer to the ViewModel layer is also written in code-Behind.
2) ViewModel is the most important part in the MVVM architecture. ViewModel contains attributes, commands, methods, events, attribute verification, and other logics for logical implementation and communication between views and models.
3) Model is the data Model we often call. It is used for data construction and data-driven. It mainly provides basic entity attributes and validation logic for each attribute.
Interaction between modules in MVVM:
ViewModel is the most important part in the MVVM architecture. It is responsible for direct communication between views and models. The understanding of ViewModel is the key to understanding MVVM. Next we will analyze ViewModel in detail.
1. the attribute of ViewModel is the source of View data, but the ViewModel layer cannot be a simple encapsulation of the Model layer, and the ViewModel layer cannot be a simple View ing of the View layer. The attributes of ViewModel can be composed of three parts: one is the copy attribute of the Model, and the other is used to control the UI status. For example, when the Disable attribute of the Button property is completed, you can use this attribute to change the Notification View to perform the corresponding UI transformation or event notification mentioned later. The third part is the parameters of some methods, you can set the parameters of these methods to the corresponding attributes bound to a control in the View, and then obtain these attributes when executing the method. Therefore, the general method does not include parameters.
2. The ViewModel command in ViewModel is used to accept the user input of the View and process it accordingly. We can also implement the same functions through methods.
3. Events in ViewModel event ViewModel are mainly used to notify the View to perform corresponding UI changes. It is usually triggered after a processing is completed, and then the View needs to perform corresponding non-business operations. Generally, the subscriber of an event in ViewModel is only a View, unless other custom non-View classes interact with each other.
4. View and ViewModel Interaction Mode
Two-way connection between the View and ViewModel models is mainly through data binding. After correct use of this design pattern, each View does not contain anything except pure XAML and a small amount of post-code, and completely achieves the separation of interface display and business logic, this allows programmers to focus more on coding. It is not necessary to ensure that the Code-Behind file is clean, because sometimes it cannot be avoided to add some UI logic or rich animation effects in Code-Behind.
5. ViewModel can also be used to hold the View status and execute any commands required by the View. Since WPF has a built-in Command mode, all UI elements such as Button controls have a Command attribute, which is the ICommand type defined by WPF. You can put these commands in ViewModel and expose them in the form of public attributes, so that View can be bound to them. This is extremely powerful because it can bind executable code in ModelView to the Button of the form.
In wpf, I think it is important:
1. Bind data using the DataContext attribute or ItemSource attribute.
2. Use the Command event or bind the Command to the trigger.
3. Using templates not only facilitates style management, but also facilitates later changes
4. The INotifyPropertyChanged interface must be implemented to notify the interface when attributes are changed.
The above is not original, but an excerpt that is simple and easy to understand during the learning process. I would like to share with you.