MVVM--Callmethodaction and Invokecommandaction

Source: Internet
Author: User

Calculation, engaged in the development of Silverlight and WPF also has more than 1 years of time, although the time is not long, although there is no outstanding achievements, but also the general feeling.

However, there has been no serious research and use of MVVM from scratch, although it is considered to be the best architectural practice for Silverlight and WPF development.

I think there are some reasons for this, like the general beginning we will not be optimistic about unit testing. Until one day you realize its charm, its benefits.

Recent projects have had to adopt the MVVM pattern: The UI is not fixed, and even the service is not fixed, but it cannot wait until these are done to begin our development work.

Therefore, the pain is determined to study the model of MVVM, in the learning process, found some problems. MVVM is not only used because it requires new thinking, making the interaction between view and viewmodel more cumbersome. There is another reason for this, which is that the reference is not complete.

General online information is a simple tutorial, how to use the command, how to use the binding method to build the application. These are almost all from the articles of the WPF article by John Smith.

In fact, in development, there are other problems, such as page switching, UI events, and simple examples where we often don't know the responsibilities and relationships between the modules. Because it cannot be effectively practiced, it is considered to affect development efficiency without being adopted.

Here, I put my knowledge of the summary into a complete example, there are source code, the source code contains a more complete example, not only the use of data binding and command, but also includes UI events and UI switching. I believe I can explain most of the problems we encounter.

Of course, if you have better practices and suggestions, you are welcome to discuss ....

Introduction to 1.MVVM Design Patterns

The design pattern for MVVM was first mentioned by Microsoft's WPF and Silverlight architect John Gossman in his blog in 2005. The following is a link to this article:

Http://blogs.msdn.com/b/johngossman/archive/2005/10/08/478683.aspx

The MVVM design pattern is based on the structure of MVC, which separates UI and logic. The traditional. NET platform for software development such as ASP. Wpf/silverlight and most are based on codebehind, we tend to write all the code in the background code files, such as UI operations, business logic operations, IO, data service invocation and so on. Although this is superficially beneficial to "development efficiency", in fact the project structure is not clear, the various modules tightly coupled, not conducive to expansion, not conducive to testing.

Mv-x's ideas provide a good practice for architectures under the. NET Platform. It allows us to build software projects that are more scalable, well structured, clearly defined, and easy to test.

But there is no standard practice for the MVVM model yet, and Microsoft has not yet given a relatively standard solution. The main discussion in the community today is the idea of MVVM. In the actual development process has formed several different styles. Among them, Josh Smith's article influenced the larger:

Http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

The main reference in this article is also the idea of John Smith.

2. Benefits of using the MVVM design pattern

Schemas that use MVVM in Silverlight or WPF can achieve the following benefits:

1. The project can be tested higher, so that unit testing can be performed

2. Completely separate UI and business design, view and UnitTest are just two different forms of consumer ViewModel

3. Help us differentiate and which are UI actions, which are business operations, rather than confuse them all in codebehind

3. Introduction to the project structure

The following is the structure of the sample project:

The following are the links between the modules:

4.wpfmvvmsample.foundation

Provides some basic class definitions.

Responsibilities of 5.Model

The model mainly provides the properties of the underlying entity and the validation logic for each property.

The model does not contain calls to data, but it can contain simple non-data-calling operations, such as generating serial numbers or merging fields.

For WCF-generated client proxy classes, there should be a class structure definition corresponding to it in models.

Model does not depend on any project.

6.IService and Services and Servicetest

IService is the service interface for all network data Services or IO operations.

The data access method in IService is primarily asynchronous, see the reference example.

Service is the real data service access class and is the implementation of IService. Servicetest is the implementation of IService for testing ViewModel

Responsibilities of 7.ViewModel

ViewModel is the most important part of the MVVM architecture, and ViewModel contains attributes, commands, methods, events, property validation and other logic. To better interact with the view and model to meet the MVVM architecture, ViewModel's design needs to be aware of some things or constraints:

ViewModel Properties : The ViewModel property is the source of the view data. These properties can be made up of three parts:

Part is the copy property of the model.

The other part is used to control the UI state. For example, a pop-up control may have a Isclose property, which can be changed by this property change notification view to make the corresponding UI transform or the event notification mentioned later when the operation is complete.

The third part is the parameters of some methods, you can set the parameters of these methods to the corresponding properties bound to a control in view, and then to execute the method when the properties are obtained, so the general method does not contain parameters.

viewmodel Command : The command in ViewModel is used to accept the user input of the view and to do the corresponding processing. We can also implement the same functionality by means of methods.

ViewModel Events : Events in ViewModel are primarily used to inform the view to make the appropriate UI transformations. It is usually triggered after a process has been completed, and then the view is required to make the corresponding non-business operation. So the subscribers to the events in the general ViewModel are only the view, unless the interaction between other custom non-view classes.

ViewModel Method : Some events are not directly provided by the command call, such as custom events. At this time we can use Callmethodaction to invoke the method in ViewModel to complete the corresponding operation.

8.View and Codebehind

command in View: Controls such as the button in view can be directly bound to the Command property called command in ViewModel

View uses callmethodaction : Some controls that do not support command, you can use a callmethodaction trigger to execute methods in ViewModel. Note that the methods often contain parameters that can be bound to related input controls, such as a TextBox, by setting the appropriate properties for ViewModel.

DataTrigger used in view: In addition to the model properties, there is also a state attribute, which is often the way ViewModel notifies the view through property changes to make related UI actions, such as triggering an animation, or toggling control state, and so on. Some triggers can be used at this time to make the corresponding UI transformations when the status values are different.

The ViewModel context of the child view is initialized in the view's codebehind : The view is typically called by the parent view, so the ViewModel of the view is typically initialized by the parent view. For example, when you click the People button and need to display the view of the people, the main frame initializes the ViewModel of the people and displays the view of the people.

UI events for subscribing to Child view in view Codebehind : In addition to triggering a trigger in view with a change in the State property, it is a choice to subscribe to ViewModel UI events in view's codebehind.

9.View and ViewModel Interactive mode summary

From the above analysis we can summarize the view and ViewModel Interactive mode:

1. Parent View initializes child ViewModel in Codebehind

2. Parent view subscribes to child ViewModel UI events in Codebehind

3. The parent view assigns the child ViewModel to the DataContext of the child view and displays the child view

4. The parent view calls the child ViewModel method that gets the data, and the child ViewModel invokes the data service to fetch the data

5. ViewModel data is passed through the binding to the view

6. View accepts user input and gives ViewModel a business deal by command or callmethodaction

7. ViewModel The UI event is triggered after processing is complete or the State property is notified to the parent view

8. The parent view makes a view transform to a new interface

10. Dependency Injection

ViewModel's job is to provide the data to the view and invoke the underlying data service.

In order to relieve the ViewModel and BP coupling, add a layer of IService interface definition, which also allows us to construct different IService implementations to test the ViewModel.

But when the view is using ViewModel, ViewModel must use the IService object, so the dependency injection is used here. Use dependency injection to completely remove the view and ViewModel dependency on the service.

Sample project source code Download:

Http://files.cnblogs.com/hielvis/WpfMVVMSample.rar

Reprint: The King of Happiness http://www.cnblogs.com/hielvis/archive/2011/03/22/1991959.html

MVVM--Callmethodaction and Invokecommandaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.