官术网_书友最值得收藏!

View Models

The View Models can be explained easily; each View Model provides its associated View with all of the data and functionality that it requires. In some ways, they can be considered to be similar to the old Windows Forms code behind files, except that they have no direct relationship with the View that they are serving. A better analogy, if you're familiar with MVC, would be that they are similar to the Controllers in the Model-View-Controller (MVC) software architectural pattern. In fact, in his blog, John describes the MVVM pattern as being a variation of the MVC pattern.

They have two-way connections with the Model component in order to access and update the data that the Views require, and often, they transform that data in some way to make it easier to display and interact within the UI. They also have two-way connections with the Views through data binding and property change notification. In short, View Models form the bridge between the Model and the View, which otherwise have no connection to each other.

However, it should be noted that the View Models are only loosely connected to the Views and Model components through their data binding and interfaces. The beauty of this pattern enables each element to be able to function independently from each other.

To maintain the separation between the View Models and the View, we avoid declaring any properties of UI-related types in the View Model. We don't want any references to UI-related DLLs in our View Models project, and so we make use of custom IValueConverter implementations to convert them to primitive types. For example, we might convert Visibility objects from the UI to plain bool values or convert the selection of some colored Brush objects to an Enum instance that is safe to use in the View Model. We will see several examples of converters throughout this book, but for now, let's continue.

主站蜘蛛池模板: 葵青区| 土默特左旗| 崇州市| 汉川市| 将乐县| 应城市| 马公市| 五指山市| 丘北县| 奉化市| 资源县| 肇东市| 嘉定区| 深州市| 温宿县| 辉县市| 玉树县| 福清市| 青川县| 扬中市| 锦屏县| 芜湖市| 澄迈县| 丰台区| 永福县| 乌拉特后旗| 吉安县| 三原县| 隆化县| 分宜县| 靖西县| 桐乡市| 修水县| 当涂县| 威海市| 莆田市| 平原县| 罗源县| 鹤岗市| 新和县| 武山县|