[Original translation] [mvvm topic] _ advanced mvvm Chapter 2

Source: Internet
Author: User

Advanced mvvm [full set of Chinese and English]:

Original article (e): Advance mvvm

Chapter 1: Full-text introduction

Chapter 2: Brief Introduction to WPF and mvvm

Chapter 3: view architecture

Chapter 4: Overview of viewmodel Architecture

Chapter 5: animated transitions

Chapter 6: unrestricted revocation under animated transitions

Chapter 7: Game-over dialog box

Chapter 8: Review

Author: kingmoon

Reprinted Please note:Http://www.cnblogs.com/kingmoon

Chapter 2 _ brief overview of WPF and mvvm

This chapter aims to help beginners of WPF or mvvm understand the entire WPF and mvvm blueprint. of course, this part is also very interesting for veterans. You can review similar topics from different perspectives. all subsequent chapters are intended for those who have a general understanding of WPF and mvvm. if you do not want to or do not need to read this chapter, you can skip it directly.

WPF

Microsoft's Windows Presentation fundation (WPF) is running in. net Framework's user interface programming platform, which has many features. We don't need to know every feature. On the contrary, we should pay attention to the key features about how bubble burst works.

For this book, we need to understand the most important feature: data binding system (binding ). I have also seen a few developers develop from winform to WPF because of their unconfident data binding capabilities. data Binding automatically moves data objects from one place to another. you can specify the time, method, and reason for triggering a data binding so that it can transmit data between the target and source points. therefore, it is possible to bind the attributes of a UI element to the attributes of another UI element. you can even bind an element to yourself. in addition, the data converter can bind different types of data between two different attributes. binding can be performed onCodeCan also be created in the markup language.

In WPF, the user interface is described as a typical Markup Language-XAML. It is an application.ProgramExtension language representative. it is based on XML format and used to declare. net object. and configure these parameters through the property value and event handle. net object. there is usually a C # code file behind the XAML file. you can process the declared methods in XAML in this code file. this XAML file and code file both contain one or more classes. this means that you can access the declared objects in XAML through code files, and vice versa.

Some controls, such as buttons, allow you to bypass the code to set their command attributes. if you set the command attribute of a button to an object that has implemented the icommand interface, the user will automatically execute these commands when you click this button. as we will see next, declaring a binding command will help simplify your application design.

Arranging various UI elements into a variety of la S is required for user interface development. in WPF, You can implement numerous la s by using the Panel that it supports. A "Panel" refers to an element container that knows how to place its sub-elements. it achieves this based on their size and layout logic. some "panels" also adjust the size of their child elements to achieve the expected layout. WPF contains several general abstract panel classes. for example: grid, stackpanle. if you need to use coordinates to represent positions, the canvas panel is the best choice.

some objects must be displayed in a simple application, WPF treats these object lists as first-class citizen (I don't know how to translate them here) and provides you with itemscontrol. many public controls are derived from itemscontrol. for example: ListBox and listview. they have an itemssource attribute that can be set to any object set. the uielements derived from itemscontrol can render these object sets. you can specify the itemtemplate attribute of itemscontrol as the custom ememplate to render the table items in these list controls in any way you want. in more complex cases, you can use an itemtemplateselector to automatically select a datatemplate based on values and data objects. another feature of itemscontrol is to allow you to specify the layout of child elements on a specific layout panel. you can use any layout panel, including the user panel, as itemspanel, to implement the layout policy you want to layout the child elements of these itemscontrol.

Another thing you need to understand is how WPF makes bubbleburst significant. It uses a "retained" rendering system (it uses a "retained" RenderingSystem ). unlike Windows Forms and other Windows Forms UI platforms based on hwnd (window handle in MFC), in WPF, you almost do not need to write code to draw windows. he opposed this "destructive" (destructive) Rendering System. WPF retains the rendering system cache vector rendering instructions and Intelligent Processing. This work is transparent to you (WPF's retained rendering system caches Vector drawing instructions and intelligently manages the job of handling things like region invalidations for you ). you only need to provide WPF the description of the interface you want to draw, through advanced objects, such as elliptical, textblock. it calculates where and when the object needs to be painted on the screen.

For more WPF:

If you want to learn more about WPF before reading it, consider accessing the following link.

Introduction to Windows Presentation Foundation

Http://msdn.microsoft.com/en-us/library/aa970268.aspx

WPF architecture.

Http://msdn.microsoft.com/en-us/library/ms750441.aspx

WPF guidance.

Http://joshsmithonwpf.wordpress.com/a-guided-tour-of-wpf/

The Custom Data of WPF is displayed through databinding.

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

Itemcontrol

Http://drwpf.com/blog/2008/03/25/itemscontrol-i-is-for-item-container/

 

Mvvm

It is often said that a great road passes Rome. today, all the best practices of WPF and Silverlight are directed to the design mode of Model-View-viewmodel. mvvm has become a way to discuss and design and implement the WPF and Silverlight programs. like all other design patterns, it provides a series of practical guidance for you to write software that sounds very structured and maintainable and easy to understand. like all other design patterns. it provides you with a common vocabulary that you can discuss with your technical team. it comes from the Model-View-presenter design mode. but as a developer, You can simplify your life by using this design pattern (it takes root in the Model-View-presenter design pattern, but diverges in ways that enable you to leverage capabilities of the UI platform to simplify your life as a developer is too difficult to translate)

Mvvm declares three object directories. The model object contains the data modified by the user. it can also contain business processing rules, input verification, change tracking, and other things about your system data. views, which is completely visualized. A view is a UI control that displays data, allowing users to modify the state of a program by entering the device (keyboard, mouse) to display images, galleries, or something you want to see on the screen. so far, everything is normal. now let's discuss the middle layer: viewmodel.

Viewmodel is a view of a model.

this topic cannot be clearly described. let's take a deeper look. first. A viewmodel is not just a code hide for creating a new view (note. XAML. CS ). this is a common misunderstanding. if one wants viewmodels to be a hidden code file in a new window. they may have written too many winform programs. when viewmodels is used, your views usually have a code hidden file. however, this viewmodel is an abstract user interface. it should have nothing to do with the UI element. the logic code that changes to another view when processing the selected object should appear in the view code hiding (logic that deals specifically with objects scoped to a participating view shoshould exist in that view' s Code-behind)

You may wonder what viewmodels is ". there are several reasons to create and use viewmodels. the most important reason is that you can use the user interface of the program as a logical system so that you can design it into an independent project. Other parts of the application can also be used (the original article is hard to understand: the most important reason is that it allows you to treat the user interface of an application as a logical system that can be designed with the same quality of engineering and object orientation that you apply to other parts of your application ). it makes it easy for you to write unit tests for user interface functions without having to confuse with unit tests for these complicated UIS. it means that the views that render your viewmodel can be modified or replaced when you need them, and no viewmodel class needs to be modified. this means that you can use a framework like MEF to dynamically generate your viewmodel. it also means that it is easy to support plug-in Architecture in your user interface layout.

the basic mechanism involved in creating mvvm applications is Data Binding and commands. viewmodels provides some attributes for the bound views. includes the returned command objects. after the viewmodel attribute is changed. A view bound to viewmodel will receive a modification trigger. When viewmodel triggers its propertychanged event (the inotifypropertychanged interface with only one function member ). the data binding system automatically obtains new values from the changed attributes and updates the binding attributes in the view. similarly, changes to data in views are bound to viewmodel. when a user clicks a button bound with a command to viewmodel, this command will be executed and allow viewmodel to process user interaction. if viewmodel needs to provide a set of objects that can be modified for the view, VM can use the observablecollection class to easily get notifications about the modification of the set. (Note: If you want to bind a set to a list control of view, it is best to derive from observablecollection . because it has an automatic modification notification. when the set is modified. it will notify the interface to update. you do not need to do anything .)

Makes the view and viewmodel bonded together, the most common is: datacontext attribute. it is an attribute inherited by all visual elements. when a view is created. the datacontext can be viewmodel. in this way, the elements in your view can be easily bound to the elements in the viewmodel.(This sentence is very important ). this is a fast and easy way to implement. some prefer to put their viewmodel into the resource dictionary. element Binding is referenced by resources. this technology works well with the expression blend tool of microsolf (Note: I don't know whether it is datacontext or resource dictionary, or the binding method of view and viewmodel ).

Learn more about mvvm:

If you want to learn more about mvvm before reading it, consider the following links:

WPF applications in mvvm design mode.

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

The viewmodel design mode is simpler to write the WPF Treeview.

Http://www.codeproject.com/Kb/Wpf/TreeviewWithViewModel.aspx

Master mvvm under WPF and Silverlight.

Http://weblogs.asp.net/craigshoemaker/archive/2009/02/26/hands-on-model-view-viewmodel-mvvm-for-silverlight-and-wpf.aspx

Via Karl shifflett M-V-VM

Http://karlshifflett.wordpress.com/mvvm/

Mvvm light toolkit via Laurent Bugnion

Http://www.galasoft.ch/mvvm/

Josh Smith's mvvm organization (the most authoritative mvvm website)

Http://mvvmfoundation.codeplex.com/

 

Please note:

Http://www.cnblogs.com/kingmoon/

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.