DCSIMG
Windows Presentation Foundation 4 Plumbing and Internals - Essential WPF

Windows Presentation Foundation 4 Plumbing and Internals

In this session we saw the inside scoop on how Windows Presentation Foundation (WPF) powers all its rich services – like layout, databinding, and animation. There is a lot of heavy lifting done for you under the hood; in this session, we crack open these system components, including the trees (visual, logical, and inheritance), the property system and its change notification, and a few of the routing systems that all messages go through. Bring your hard-hat for this one!

WPF Involves lots of interacting concepts

  • Layout, Animation, Styles, Templates, Binding and Events

Windows constructore calling InitializeComponents, but layout and template expanding and other binding related are differed. We can’t assume that layout worked in the ctor.

Where are properties set?

Animated Value, Local Value, Setter in style trigger, Setter in style, Inherited value

Property values are often sought rather than set!

  • SetValue sets local value
  • GetValue get effective value

Set may destroy binding. SetCurrentValue instead of SetValue will do the job.

Binding in general

  • DataContext is inherited – Data template instances given explicit context
  • Some properties bound two-way by default
  • CollectionView – implicitly created when binding to a collection. Tracks “current” item
  • Binding expressions now work with .NET dynamic type

Inheritance

The logical tree is the containment part of the tree.

  • Templates expanded lazily during layout
  • Inherited properties trace inheritance context
    • First preference is logical parent
    • When not present uses visual parent
  • Freezable subtypes have a special inheritance context
    • Shared freezables (using the XAML x:Shared keyword) inherit based on where they are declared, non-shared based on where they are used
  • In WPF 4.0, InputBinding is now freezable – can take advantages of the inherited context

Dispatcher serializes everything

  • Dispatcher runs on UI thread
    • All DispatcherObjects associated with dispatcher, single-threaded
    • Processes a prioritized work queue

We can break long tasks into Bite-Sized Pieces.

  • Use Dispatcher.BeingInvoke, process work in priority order
  • Priority can be changed after scheduling
  • DispatcherTimer schedules work as inactive, uses Win32 time to update

Use threads:

  • WPF Render Thread – WPF internals only
  • UI Thread(s) – UI Construction, Event listeners, Layout, Binding etc.
  • Worker Thread Pool – 100% User Code

Long-running and blocking operations belong to worker threads. Avoid DispatcherObject in non-UI threads.

You can use BackgroundWorker to save the threading pain.

Visuals and framework elements do a lot of work. Avoid premature optimization. You can use Snoop with memory and profiling tools to investigate observable issues. Consider OnRender()

Before cutting down the number of visuals in the visual tree, try to figure out where the real problem is. WPF can handle thousands of visuals per second.

Virtualizing Visual Trees

If you replace the items panel in an ItemsControl, try to use the VirtualizationStackPanel instead of StackPanel, it performance much better. DataGrid in WPF 4.0 support VirtualizationPanel.

Published Thursday, November 19, 2009 8:57 PM by Tomer Shamam

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems