다음을 통해 공유


Windows Presentation Foundation

Note: This page is currently a stub article. Add information as you can.

 


Introduction

Windows Presentation Foundation (WPF) is a client programming interface and presentation system for building Windows applications with a new, stunning user experience, introduced for the first time in Windows Vista and .NET Framework 3.0.

The WPF core is a resolution-independent and vector-based rendering engine that is built to take advantage of modern graphics hardware. WPF extends the core with a comprehensive set of application-development features that include Extensible Application Markup Language (XAML), controls, data binding, layout, 2-D and 3-D graphics, animation, styles, templates, documents, media, text, and typography.

WPF is included in the Microsoft .NET Framework, so you can build applications that incorporate other elements of the .NET Framework class library.

Styles & Control Templates

 
One of the most powerful and important aspects of WPF to thoroughly understand is Styling and Templating.
 
A control has a Control Template, which defines what is in the control's user interface and how it is layed out.
A Control (or UserControl) exposes a number of Dependancy Properties, which can be used in binding, like TextBox.Background (colour) or TextBox.Text.
 
A Style is a collection of Dependancy Property Setters and various Trigger types, which can be applied to some (explicit style) or all (implicit style) controls of that style's TargetType
 
If you give the Style an x:Key, it is called an "explicit style" and you attach the style to selected controls. 
If you do not give the style an x:Key, so it only has a TargetType reference, it is called an "implicit style", and it will apply to all controls of that same TargetType type.
 

Triggers

 
The use of Triggers and Visual States mean UI change logic can stay in the UI, and not clutter up the code.

Whereas a Winform CheckBox would need an event and code to open a new section of the page, this can now all be done on the actual page.

With all the UI logic being handled in the UI, you can simply lift off the page and use another page with completely different logic.
 

WPF Design Patterns

There are two main styles of coding a WFP application (not including third party frameworks like Dependancy Injection frameworks).

Code Behind

This method involves simply placing all your code and event handlers in the .xaml.cs partial class file, this is the same style of coding as for WinForms. This has some benefits, as you have direct access to the controls and events.

The downsides of such tightly coupled code [in many people's opinion]:

  • Code that is not reusable
  • Difficult to refactor
  • Increased overhead of maintenance
  • Increased overhead on some aspects of development ( it's very hard to do some things using code behind ).
  • Strong dependencies which make automated tests impossible
  • Difficulty in re-skinning an application

A Winform Developer will usually start with code-behind programming, but it can encourage bad habits, like traversing the visual tree (rows and cells) of a DataGrid to read and change properties, instead of working with the source data (which was bound to the control anyway).

Model View ViewModel (MVVM)

This method involves completely separating the data and business logic from the user interface.  
Most WPF developers use the MVVM pattern and anyone new to WPF is advised to at least investigate the pattern.

Data is simply "presented" in a ViewModel, which the View (Page/Window) consumes.

The advantages are largely due to loose coupling:

  • Automated testing is far easier
  • Re-usable code
  • Easier to refactor for changes
  • Easier to maintain
  • Far more practical to use best practice such as SOLID
  • Far easier to do some things using MVVM
  • Easier to re-skin an application

Almost every feature that was lost from working within the actual UI partial class (like events) can be replaced by a command, attached property or attached behaviour.

Everything in the UI (View) is bound to a property of the ViewModel, which makes the application much more testable, as every change of the UI is represented by a neatly organised property of the VM.

MVVM is an implementation of Martin Fowler's passive view pattern.

 

Community Resources

MSDN Library Web Pages

Technical Articles

Books

Videos