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

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.

主站蜘蛛池模板: 合江县| 青岛市| 盱眙县| 玉田县| 武威市| 金川县| 江陵县| 遵化市| 子洲县| 博兴县| 万全县| 河源市| 泽普县| 开封市| 大关县| 平定县| 扶沟县| 昆明市| 浦城县| 林周县| 吴桥县| 鸡西市| 睢宁县| 奉贤区| 东兰县| 新龙县| 浦北县| 武山县| 曲靖市| 泸溪县| 广南县| 弋阳县| 界首市| 渝北区| 丹巴县| 临泉县| 古交市| 隆安县| 开封县| 杭锦后旗| 卢龙县|