1 / 28

Overview of WPF

Overview of WPF. Ivan Krivyakov Senior Managing Consultant SunGard Consulting Services E-mail: Ivan.Krivyakov@SunGard.com ivan@ikriv.com http://www.ikriv.com/demo/wpf/. Overview of WPF. What Is This Presentation About WPF for an experienced developer What makes WPF different?

lyle
Download Presentation

Overview of WPF

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. Overview of WPF Ivan Krivyakov Senior Managing Consultant SunGard Consulting Services E-mail: Ivan.Krivyakov@SunGard.com ivan@ikriv.com http://www.ikriv.com/demo/wpf/

  2. Overview of WPF What Is This Presentation About • WPF for an experienced developer • What makes WPF different? • Things to explore • Definitely not a tutorial

  3. Overview of WPF Why Is WPF Important? • It’s a major technology from a big market player • It is gaining ground in replacing Windows Forms • Many clients start including WPF knowledge as a requirement for hire • Officially released in Nov 2006 and still popular – definitely not a hype bubble

  4. Overview of WPF A Contrived Mixture Formula WPF = XAML + Data Binding + Styles

  5. Overview of WPF XAML • XML-based object definition language • Defines objects and their properties • Similar to the language of Spring.Net • Primarily used to describe UI artifacts • Can describe almost any .NET type • Limited support of generics

  6. Overview of WPF XAML Describing UI artifacts: <DockPanel LastChildFill="True"> <StackPanel Orientation="Vertical" DockPanel.Dock="Top"> <TextBlock Margin="2">Count:</TextBlock> <TextBox Margin="5" Text="{Binding Count}" /> <TextBlock Margin="2">Results:</TextBlock> </StackPanel> <ListBox Margin="5" ItemsSource="{Binding Results}" /> </DockPanel> Describing custom data classes: <my:NumericViewModel Count="5"> <my:NumericViewModel.Sequence> <my:SquareRoots /> </my:NumericViewModel.Sequence> </my:NumericViewModel>

  7. Overview of WPF XAML • Many controls accept arbitrary content • E.g. a button may contain any combination of other controls, such as text, images, and even text boxes, etc. • Thus no need for special image button or Image property on a button class

  8. Overview of WPF XAML • ImageButtonDemo sample

  9. Overview of WPF Data Binding <TextBlock Text=“{Binding Names.Count}” /> <ListBox ItemsSource=“{Binding Names}” /> • By default binds to the DataContext object of the UI element • Can bind to properties of other UI elements, static instances, etc.

  10. Overview of WPF Dependency Properties • CLR properties: get, set • WPF dependency properties: get, set, default value, change notification, validation, animation, etc. • Dependency properties are set via reflection-like API • WPF DependencyObject class has a map from property descriptors to property values • Most WPF classes derive (directly or indirectly) from DependencyObject

  11. Overview of WPF Dependency Properties The Dependency property API is somewhat bulky: public class MyStateControl : ButtonBase { public MyStateControl() : base() { } public Boolean State { get { return (Boolean)this.GetValue(StateProperty); } set { this.SetValue(StateProperty, value); } } public static readonly DependencyProperty StateProperty = DependencyProperty.Register( "State", typeof(Boolean), typeof(MyStateControl), new PropertyMetadata(false)); }

  12. Overview of WPF Dependency Properties • GetValue(), SetValue() can accept properties owned by other classes. These called “attached” properties • In XAML attached properties look like so: <DockPanel> <TextBlock DockPanel.Dock=“Top” /> </DockPanel> • “Dock” property is owned by the DockPanel class, but it is set on a TextBlock instance. This is somewhat similar to extension methods in C#

  13. Overview of WPF Data Change Notification • Binding mechanism must know when things change • Dependency properties are set via WPF-controlled methods GetValue, SetValue

  14. Overview of WPF Data Change Notification For regular properties must implement interface INotifyPropertyChanged{ event … PropertyChanged;}class MyClass : INotifyPropertyChanged{ string _data; string Data { get { return _data; } set { if (value == _data) return; _data = value; PropertyChanged(…”Data”…); } }}

  15. Overview of WPF Data Binding • Binding source: regular property or dependency property • Binding target: dependency property • Animation target: dependency property

  16. Overview of WPF Data Binding • NumericFunctions Sample

  17. Overview of WPF Data Templates • Data templates replace owner draw items in item controls like list boxes • Rule of thumb: avoid “assembling” text for UI in code. Use data templates instead

  18. Overview of WPF Data Templates • NumericFunctions sample revisited using data template

  19. Overview of WPF Styles and Control Templates • Lookless controls: behavior is separated from appearance • Behavior: control class, appearance: control style • Clock control may have Time property, and can track current time • Styles can define colors, fonts, and even completely overhaul the appearance of the control

  20. Overview of WPF Styles and Control Templates • ClockControl demo

  21. Overview of WPF WPF Binaries • Main: shipped with .NET 3.0 and later • WPF toolkit: additional controls such as data grid, date picker control • “Prism”: composite application guidance for WPF

  22. Overview of WPF Expression Blend • Writing XAML by hand, especially grid layouts, may be overwhelming • Visual Studio has limited editing capabilities • Expression Blend is a separate application for editing XAML documents • Pros and Cons

  23. Overview of WPF MVVM vs MVP Model View Presenter Model WPF Binding Mechanism View ViewModel

  24. Overview of WPF MVVM vs MVP • MVVM is a WPF version of MVP • View is pure XAML • ViewModel has data to display • ViewModel may be more detailed/redundant than business objects • ViewModel does not have a reference to the view

  25. Overview of WPF MVVM vs MVP • All communication to the view is done via bindings • Bindings take on most of the work of the presenter • ViewModel is mostly about presentation data. If there is no complex business logic, model is not necessary

  26. Overview of WPF MVVM and Commands • Out of the box WPF does not fully support MVVM • Exchanging data is easy, dealing with commands, focus, etc. is more difficult. Not having reference to the view causes problems • Default mechanism of routed commands is not compatible with MVVM, since commands are delivered to the view

  27. Overview of WPF Summary • WPF is very flexible, it has almost mathematical beauty • New approach to UI programming: binding, attached properties, MVVM • A learning curve • Fair number of gotchas and bugs • Performance is a frequent concern • Demand for WPF is on the rise

  28. Overview of WPF References • Book: “Programming WPF” by Chris Sells • WPF toolkit et al.: http://wpf.codeplex.com/ • Prism:http://compositewpf.codeplex.com/ • MVVM: http://msdn.microsoft.com/en-us/magazine/dd419663.asp

More Related