Mvvm mode in WPF: Repost in WPF Mode

Source: Internet
Author: User

Address: http://www.cnblogs.com/winkingzhang/archive/2008/12/01/1344816.htmlIntroduction

Since XAML things have become a bit complicated in trying to conceptualize MVC ubuntures 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 machisms 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 ubuntureoverview

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 blocks acts away the expensive operations, never blocking the UI thread (that is edevil !). 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 each layers. viewmodels may compose other viewmodels and datamodels. and, datamodels may be composed of other datamodels.

TheViewmodelIs 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

UsingInotifypropertychangedYou can bubble changes up the stack. the reason that public methods shocould be on the UI thread is because the model cocould call long running or async stuff which wocould 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 ofBegininvoke ()Method of the dispatcher and the paramter that specifies the priority. The systemidle in particle is interesting to be used when the dispatcher is not busy.

TheDatamodelYou 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 the 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.

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.