what went wrong? 4.0 animated valuelocal value“setter” in style triggersetter in styleinherited...

25
WPF 4 Plumbing and Internals Blake Stone Architect Microsoft Corporation PDC09-CL10

Upload: denis-casey

Post on 13-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

WPF 4 Plumbing and Internals

Blake StoneArchitectMicrosoft Corporation

PDC09-CL10

Page 2: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

WPF is Deep

> WPF involves lots of interacting concepts> Layout> Animation> Styles> Templates> Binding> Events

> Often many ways to accomplish a goal> Not necessarily direct parallels to Win32,

WinForms, or other frameworks

Page 3: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Teaching You to Fish …

> Cookbook-style programming has its place … not in this session!> You can’t reason without knowing why

something works> Focus on how WPF is intended

to be used> Design principles> Implementation strategies

> When all else fails:workarounds

Page 4: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Why Aren’t My Properties Set the Way I Expect?

Demystifying initialization and binding

What Went Wrong?

Page 5: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Window Constructor

Evaluating XAML: What Happens When?> Initialized event

> Fires during XAML processing

> Fires on leaf nodes before their parents

> Does not guarantee all properties are set

> Called during the constructor that calls InitializeComponent()

> Loaded events> Don’t count on the

ordering

InitializeComponent()

Child Constructors

Child Initialized Events

Loaded Events

Deferred Binding, Template Expansion,

Layout, etc.

Window Initialized Event

Page 6: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

When Are My Properties Set?

> It’s a trick question: property values are often sought, rather than set!> SetValue() sets local value> GetValue() returns

effective value> InvalidateProperty()

forces effective value update

> SetCurrentValue() overrides effective value, only until the next change

> Styles and animations don’t call SetValue at all!

Rough search order* for effective values:

4.0

Animated value

Local value

“Setter” in style trigger

Setter in style

Inherited value

* Final value may be modified by coercion, and animated value may depend on deeper effective value

Page 7: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Binding Magic

> DataContext is inherited> Data template instances given explicit context

> Some properties bound two-way by default> Typically user input eg: TextBox.Text> Defined by dependency property metadata

> CollectionView> Implicitly created when binding to a collection> Tracks “current” item

> ItemsControl’s IsSynchronizedWithCurrentItem enables synchronized tracking*

> Binding expressions now work with .NET dynamic type4.0

* Default of null means no synchronization for default collection views, set to true or false for more control

Page 8: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Inheritance: a Tale of Two Trees*

> 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 inherit

based on where they are declared, non-shared based on where they are used

> InputBinding is now Freezable

* The so-called “inheritance tree” doesn’t really exist, as we’ll see …

Grid

Button

TextBlock

StackPanel

ContentPresenter

<Grid DataContext="Page DataContext"> <Button> <Button.Template> <ControlTemplate TargetType= “{x.Type Button}"> <StackPanel DataContext= "Template DataContext"> <ContentPresenter /> </StackPanel> </ControlTemplate> </Button.Template> <TextBlock Text="{Binding}" /> </Button></Grid>

DC

DC

4.0

Page 9: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

My Application isn’t Responsive

Animations not smooth? Input being ignored?

What Went Wrong?

Page 10: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Dispatcher Serializes Everything

> Dispatcher runs on UI thread> All DispatcherObjects associated

with dispatcher, single-threaded*> Processes a prioritized work queue

> Long-running operations interfere with everything> Animations, input, layout, etc.

> Dispatcher reentrant on Monitor.Enter() to avoid deadlocks> Used by C#’s lock() statement> Disabled via

using(Dispatcher.DisableProcessing())

System.Object

DispatcherObject

DependencyObject

Visual

* Except Freezable subtypes, when frozen

Freezable

Page 11: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Break Long Tasks into Bite-Sized Pieces> Use

Dispatcher.BeginInvoke()> Processes work in priority

order> Priority can be changed

after scheduling> DispatcherTimer schedules

work as Inactive, uses Win32 timer to update

Send Normal

DataBind Render

Loaded Input

Background

ContextIdle

ApplicationIdle

SystemIdle

Inactive

Page 12: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Worker Thread Pool

100% User Code

Getting Thread-Savvy with BackgroundWorker

> Long-running and blocking operations belong in worker threads

> Avoid DispatcherObject in non-UI threads> INotifyPropertyChanged across threads

works> INotifyCollectionChanged does not

WPF Render Thread

WPF Internal Only

UI Thread(s)

UI construction,Event listeners,Layout, Binding,

etc.

Page 13: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

UI Thread Worker Thread Pool

DoWork

BackgroundWorker’s Three Delegates> Once configured, BackgroundWorker

coordinates work across two threads

RunWorkerCompleted

ProgressChanged

BackgroundWorker

ReportProgress()

1

2

2

3

RunWorkerAsync()

Page 14: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Why is Everything Still Sluggish?

I Don’t Have Many Controls – It Must Be WPF’s Fault!

What Went Wrong?

Page 15: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

With Great Power Comes Great Responsibility> Visuals and framework elements do a lot

> Attached properties, layout, bindings, input management, styles, and hit detection all come with a cost> Hundreds of bytes per visual> Closer to 1k for a trivial binding

> Scales well to hundreds, even tens of thousands> … but Control adds templates which can generate lots

of visuals

> Avoid premature optimization> Use Snoop with memory and performance

profiling tools to investigate observable issues> Consider OnRender() and other render data

techniques> Consider cached composition with CacheMode

property

4.0

Page 16: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Virtualizing Visual Trees

> Why create visuals that won’t be displayed?> Virtualization is sleight-of-hand typical of long,

scrolling lists> ListBox virtualizes by default, so does

DataGrid> Custom ItemsPanelTemplate for an ItemsControl

can use VirtualizingStackPanel> Layout can defeat virtualization when measuring

unconstrained sizes> VirtualizingStackPanel.VirtualizationMode

supports Recycling and GC modes

4.0

Page 17: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Only Some of My Users Have Performance Problems!

> VirtualPC and “hardware rendering” can be slower than software rendering> Software rendering can be forced with

HwndTarget.RenderMode = RenderMode.Software

> Remote desktop> Changed regions are transmitted as

bitmaps> Scrolling and animations are most

obvious> Deep magic: protected

VisualScrollableAreaClip property on Visual

4.0

Page 18: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

So Little Time, So Many Tips!

Great resources for WPF developers

Page 19: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

WPF ResourcesInformation Sources

Portal for WPF and WinForms

http://windowsclient.net/wpf

WPF Forum http://social.msdn.microsoft.com/forums/en-US/wpf/threads

WPF SDK Portal http://msdn.microsoft.com/en-us/library/ms754130.aspx

Lester Lobo on new WPF 4 features

http://blogs.msdn.com/llobo/archive/tags/New+WPF+4+features/default.aspx

WPF 4 text features http://blogs.msdn.com/text/archive/tags/WPF+4.0/default.aspx

Mike Hillberg’s Blog http://blogs.msdn.com/mikehillbergTools

Snoop http://www.blois.us/Snoop

Mole for Visual Studio http://www.codeproject.com/KB/WPF/MoleForWPF.aspx

WPF Performance Suite http://windowsclient.net/wpf/perf/wpf-perf-tool.aspx

WPF Toolkit http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29117

Page 20: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

WPF Sessions @ PDC09> PDC09-CL09 How Microsoft Visual Studio 2010 Was Built with Windows

Presentation Foundation 4 TUE 11/17/2009, 11:00AM - 12:00PM, Room 502A

> PDC09-CL11 Advanced Windows Presentation Foundation Application Performance Tuning and Analysis TUE 11/17/2009, 4:30PM - 5:30PM, Room Petree Hall D)

> CHALK TALK Deep Dive into WPF4 Multi-Touch APIs WED 11/18/2009 11:00PM - 12:00PM, The Big Room

> PDC09-CL31 Mastering WPF Graphics and Beyond WED 11/18/2009, 12:30-1:15PM, Room Hall E - 151

> PDC09-CL10 Windows Presentation Foundation 4 Plumbing and Internals THUR 11/19/2009, 10:00AM - 11:00AM, Room Hall E – 151

> PDC09-CL27 Multi-Touch on Microsoft Surface and Windows 7 for .NET Developers THUR 11/19/2009 , 11:30AM - 12:30PM, Room Petree Hall C

> PDC09-CL24 XAML Futures in Microsoft .NET Framework, Microsoft Silverlight and Tools, THUR 11/19/2009 , 1:45PM - 2:45PM, Room Hall F - 153

> MORE WPF 4 @ WPF Community site http://connect.microsoft.com/wpf

Page 21: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Ask the Experts!

John Gossman, Mike HillbergArchitectsWindows Presentation Foundation

Questions & Answers

Page 22: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

YOUR FEEDBACK IS IMPORTANT TO US!

Please fill out session evaluation

forms online atMicrosoftPDC.com

Page 23: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

Learn More On Channel 9

> Expand your PDC experience through Channel 9

> Explore videos, hands-on labs, sample code and demos through the new Channel 9 training courses

channel9.msdn.com/learnBuilt by Developers for Developers….

Page 24: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Page 25: What Went Wrong? 4.0 Animated valueLocal value“Setter” in style triggerSetter in styleInherited value