WPF模式思考 (zt)

摘要:
简介自从eXAMLT开始,在尝试为Windows应用程序设计MVC架构的过程中变得有点复杂。Web和Win之间的差距如箭头所示,以及新的WPF将增加多种可能性

Introduction

Since XAML things have become a bit complicated in trying to conceptualize MVC architectures for Windows applications. The gap between web and win is narrowing and the whole WPF thing adds diverse possibilities to one’s toobox. What to use? Model-view-controler, the model-view-presenter or the new paradigm called model-view-viewmodel?

I have tried to understand what it’s all about and this is what I found out.

What’s the problem?

WPF has changed a few things:

  • through declarative programming (i.e. XAML) a lot of plumbing is created for you which especially in the context of databinding saves a lot of code. Another area is e.g. animations and styling; what previously had to be programmed in C# is now possible inside XAML through triggers or events.
  • in the MVC pattern the View is presenting the data from the Model but now XAML does this job and sometimes need things like value converters or the ObservableCollection to update the presentation
  • user controls in WPF allow you to define a default generic.xaml style which can be overriden when used inside an application, i.e. styling of existing controls is a new feature which adds flexibility to the presentation of the Model
  • XAML allows a wide variety of mechanisms to change things or to react to user actions; through the ICommand that is now defined in the framework, through triggers in XAML, through animations, through event bubbling or tunneling (which is also new)

The MVVM architecture

Overview

MVVM

DataModel

DataModel is responsible for exposing data in a way that is easily consumable by WPF. All of its public APIs must be called on the UI thread only. It must implement INotifyPropertyChanged and/or INotifyCollectionChanged as appropriate. When data is expensive to fetch, it abstracts away the expensive operations, never blocking the UI thread (that is evil!). It also keeps the data “live” and can be used to combine data from multiple sources. These sorts of classes are fairly straightforward to unit test.

ViewModel

A ViewModel is a model for a view in the application (duh!). It exposes data relevant to the view and exposes the behaviors for the views, usually with Commands. The model is fairly specific to a view in the application, but does not subclass from any WPF classes or make assumptions about the UI that will be bound to it. Since they are separate from the actual UI, these classes are also relatively straightforward to unit test.

View

A View is the actual UI behind a view in the application. The pattern we use is to set the DataContext of a view to its ViewModel. This makes it easy to get to the ViewModel through binding. It also matches the DataTemplate/Data pattern of WPF. Ideally, the view can be implemented purely as Xaml with no code behind. The attached property trick comes in very handy for this.

The lines between DataModels and ViewModels can be blurry. DataModels are often shown in the UI with some DataTemplate, which isn’t really so different than the way we use ViewModels. However, the distinction usually makes sense in practice. I also want to point out that there’s often composition at many layers. ViewModels may compose other ViewModels and DataModels. And, DataModels may be composed of other DataModels.

The ViewModel is a model of the view. That means: You want to DataBind a property from
your DataObject (model) to a property from your ViewObject (view) but you sometimes cannot bind directly to a CLR property of the model (because of converting or calculating). This is when ViewModel comes into play. It propagates the already calculated or converted value from your model, so you can bind this property directly to the view property.

The main thrust of the Model/View/ViewModel architecture seems to be that on top of the data (”the Model”), there’s another layer of non-visual components (”the ViewModel”) that map the concepts of the data more closely to the concepts of the view of the data (”the View”). It’s the ViewModel that the View binds to, not the Model directly.

The model

Using the INotifyPropertyChanged you can bubble changes up the stack. The reason that public methods should be on the UI thread is because the model could call long running or async stuff which would block the UI, though there are methods to let the UI thread handle property changes from a separate thread. See the doc on the Dispatcher object and the WPF threading model for more on this. Note in this context that you can let things happen in the background by means of the BeginInvoke() method of the Dispatcher and the paramter that specifies the priority. The SystemIdle in particular is interesting to be used when the Dispatcher is not busy.

The DataModel you can find in the download is mimiced from the Dan Crevier’s sample and can serve as an abstract base class for your own models.

Dispatcher things

The DispatcherTimer is reevaluated at the top of every Dispatcher loop.

Timers are not guaranteed to execute exactly when the time interval occurs, but are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the Dispatcher queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.

If a System.Timers.Timer is used in a WPF application, it is worth noting that the System.Timers.Timer runs on a different thread then the user interface (UI) thread. In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of the user interface (UI) thread using Invoke or BeginInvoke. For an example of using a System.Timers.Timer, see the Disable Command Source Via System Timer sample. Reasons for using a DispatcherTimer opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set on the DispatcherTimer.

A DispatcherTimer will keep an object alive whenever the object’s methods are bound to the timer.

So, the right way to schedule things inside the WPF UI is something like;
[csharp]
private DispatcherTimer _timer;
timer = new DispatcherTimer(DispatcherPriority.Background);
timer.Interval = TimeSpan.FromMinutes(5);
timer.Tick += delegate { ScheduleUpdate(); };
timer.Start();

[/csharp]
the timer is injected implicitly in the thread associated to the dispatcher of the UI.

References

免责声明:文章转载自《WPF模式思考 (zt)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇jquery动态添加列表后样式失效解决方式NanUI for Winform发布,让Winform界面设计拥有无限可能下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF 基础学习笔记

学习笔记: WPF中对于控件名字的命名,可在code中找到,例如如下:x:Name=“MassText” ... 如何获取WPF richTextBox的text?有别于winform,比较复杂。 string richText1 = new TextRange(RichTextBox1.Document.ContentStart, RichTextBo...

WPF学习笔记二 WPF命中测试

概述: WPF中的Canvas是常用的一个绘图控件,可以方便地在Canvas中添加我们需要处理的各种元素如:图片、文字等。但Canvas中元素增加到一定数量,并且有重合的时候,我们如何通过在Canvas中点击鼠标,获得我们想要的元素,然后再对该元素做出相应的控制? 命中测试,可以很好地解决这个问题 本文目的: 使用命中测试,选取Canvas中相应Ele...

DevExpress WPF界面控件

DevExpress WPF套件提供了Reveal Highlight和Acrylic效果 - 这些外观选项旨在模仿Windows UI应用程序的外观和样式。 DevExpress WPF v21.1高速下载 Reveal Highlight 使用RevealHighlightOptions静态类添加Reveal Highlight效果,您可以将此效果添加...

WPF 中图标路径问题

给 WPF 添加图标有两种方式,一是绝对路径方式,二是相对路径方式。第一种方式操作简单,只需将要添加的图标的路径赋给 Source 即可。但是,这种绝对路径方式存在一个严重的弊端,就是当工程文件移植到其它地方时,这些绝对路径就会失效,需要多次重复修改才行。而相对路径方式就解决了工程文件移植给图标路径带来的失效问题。 绝对路径很简单,举个例子,给应用添加一...

[WPF](小结2)DataGrid嵌套DataGrid(也叫主从表)

DataGrid嵌套DataGrid(也叫主从表),效果为:单击表中某项后,从中间展开一个新表,总表绑定的是题目类大集合,从表绑定的是对应的选项集合. 第一:选构建题目类,再建一个选项类,题目类集合中的每个项包含一个选项类集合,即数组嵌套数组,C#语句如下: (为方便看清语句,类直接写在主程序中) C#代码如下: usingSystem; using...

[转]Windows8的WPF技术与HTML5的比较

Windows8风格程序支持使用WPF和HTML两种方式进行编程。本人之前曾使用WPF开发过概念版QQ,现在又在使用HTML5。两种技术都略懂,略懂。所以将两者做了个比较。虽然WPF的命运多舛,应用不太广泛,但技术本身还是有很多亮点的。值得我们学习一下,开阔眼界。   严格说WPF与Silverlight都属于.Net技术体系下新一代的界面技术,都使用标...