1 / 25

WPF 4 Plumbing and Internals

PDC09-CL10. WPF 4 Plumbing and Internals. Blake Stone Architect Microsoft Corporation. WPF is Deep. WPF involves lots of interacting concepts Layout Animation Styles Templates Binding Events Often many ways to accomplish a goal

grady
Télécharger la présentation

WPF 4 Plumbing and Internals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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 intendedto be used • Design principles • Implementation strategies • When all else fails:workarounds

  4. Why Aren’t My Properties Set the Way I Expect? What Went Wrong? Demystifying initialization and binding

  5. Evaluating XAML: What Happens When? Window Constructor • 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 Window Initialized Event Deferred Binding, Template Expansion, Layout, etc. Loaded Events

  6. 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 * Final value may be modified by coercion, and animated value may depend on deeper effective value

  7. 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’sIsSynchronizedWithCurrentItem enables synchronized tracking* • Binding expressions now work with .NET dynamic type 4.0 * Default of null means no synchronization for default collection views, set to true or false for more control

  8. 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 <GridDataContext="Page DataContext"> <Button> <Button.Template> <ControlTemplateTargetType= “{x.TypeButton}"> <StackPanelDataContext= "Template DataContext"><ContentPresenter /> </StackPanel> </ControlTemplate></Button.Template><TextBlock Text="{Binding}" /> </Button> </Grid> Grid DC 4.0 Button StackPanel DC ContentPresenter TextBlock * The so-called “inheritance tree” doesn’t really exist, as we’ll see …

  9. My Application isn’t Responsive What Went Wrong? Animations not smooth? Input being ignored?

  10. Dispatcher Serializes Everything • Dispatcher runs on UI thread • All DispatcherObjectsassociated 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 Freezable Visual * Except Freezable subtypes, when frozen

  11. 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

  12. Getting Thread-Savvy with BackgroundWorker • Long-running and blocking operations belong in worker threads • Avoid DispatcherObjectin non-UI threads • INotifyPropertyChanged across threads works • INotifyCollectionChanged does not Worker Thread Pool 100% User Code UI Thread(s) UI construction, Event listeners, Layout, Binding, etc. WPF Render Thread WPF Internal Only

  13. BackgroundWorker’sThree Delegates • Once configured, BackgroundWorker coordinates work across two threads RunWorkerAsync() Worker Thread Pool UI Thread ProgressChanged 2 BackgroundWorker DoWork 1 ReportProgress() 2 RunWorkerCompleted 3

  14. Why is Everything Still Sluggish? What Went Wrong? I Don’t Have Many Controls – It Must Be WPF’s Fault!

  15. 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

  16. 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.VirtualizationModesupports Recycling and GC modes 4.0

  17. 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

  18. So Little Time, So Many Tips! Great resources for WPF developers

  19. WPF Resources

  20. WPF Sessions @ PDC09 • PDC09-CL09How Microsoft Visual Studio 2010 Was Built with Windows Presentation Foundation 4 TUE 11/17/2009, 11:00AM - 12:00PM, Room 502A • PDC09-CL11Advanced 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-CL10Windows Presentation Foundation 4 Plumbing and Internals THUR 11/19/2009, 10:00AM - 11:00AM, Room Hall E – 151 • PDC09-CL27Multi-Touch on Microsoft Surface and Windows 7 for .NET Developers THUR 11/19/2009 , 11:30AM - 12:30PM, Room Petree Hall C • PDC09-CL24XAML 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 sitehttp://connect.microsoft.com/wpf

  21. Ask the Experts! Questions & Answers John Gossman, Mike Hillberg Architects Windows Presentation Foundation

  22. YOUR FEEDBACK IS IMPORTANT TO US! Please fill out session evaluation forms online at MicrosoftPDC.com

  23. 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/learn Built by Developers for Developers….

More Related