1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a...

85
1 Introduction to WPF Noea / PQC - 2008 Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation

Transcript of 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a...

Page 1: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

1 Introduction to WPF Noea / PQC - 2008Web

Introduction to WPFIntroduction to WPF

•Based on a Microsoft presentation

Page 2: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

2 Introduction to WPF Noea / PQC - 2008Web

.NET At The Core

Page 3: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

3 Introduction to WPF Noea / PQC - 2008Web

Topics

• Windows Presentation Foundation– Overview– 2D

– Controls and Layout

– Styles, Templates & Resources

– Data Binding

– Animation

Page 4: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

Overview

Page 5: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

5 Introduction to WPF Noea / PQC - 2008Web

Now

• GDI (20 years), GDI+, WinForms• DirectX (11 years), Direct3D• Quartz, DirectShow (8 years)

• Problems– Showing their age– Each API is different– Mixing APIs is challenging

Page 6: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

6 Introduction to WPF Noea / PQC - 2008Web

Next Gen

• WPF – replaces GDI• Direct3D – large games, used by WPF• Media Foundation – ultimately will replace DirectShow

• MCML –markup language for Media Center Edition applications

• XNA – small games

Page 7: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

7 Introduction to WPF Noea / PQC - 2008Web

WPF

• Compositing– UI, Documents, Media, 3D, Browser, …

• Declarative programming with XAML markup

• For Designers and Developers• Rewritten from scratch

– Built on top of Direct3D– Hardware accelerated– Vector based– Resolution independent– Retained graphics

Page 8: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

8 Introduction to WPF Noea / PQC - 2008Web

WPF Vision

• Integrated, vector-based composition engine

– Utilizing the power of the PC throughout the graphics stack

• Unified approach to UI, Documents, and Media

– Integration as part of development and experience

• Declarative programming– Bringing designers directly into

application development

• Ease of deployment– Allowing administrators to deploy and

manage applications securely

Page 9: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

9 Introduction to WPF Noea / PQC - 2008Web

Designer Developer

Developer Designer Collaboration

Page 10: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

10 Introduction to WPF Noea / PQC - 2008Web

XAML

• XML for Applications Markup Language

<Button Name="button1">Click Me!

</Button>

Button button1 = new Button();button1.Content = "Click Me!";

Page 11: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

11 Introduction to WPF Noea / PQC - 2008Web

Properties as Attributes or Elements

<Button Content="Click Me!" Background="LightGreen" />

<Button> <Button.Background> LightGreen </Button.Background> Click Me!</Button>

Page 12: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

12 Introduction to WPF Noea / PQC - 2008Web

Attached Properties

<Canvas> <Button Canvas.Top="30" Canvas.Left="40"> Click Me! </Button></Canvas>

Page 13: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

2D Graphics

Page 14: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

14 Introduction to WPF Noea / PQC - 2008Web

Shapes

• Elements of UI tree– Just like controls and other elements

Rectangle

Ellipse

Polyline

Polygon

Path

Page 15: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

15 Introduction to WPF Noea / PQC - 2008Web

Shapes Example

<Canvas Width="100" Height="100"> <Ellipse Fill="Yellow" Stroke="Black" StrokeThickness="7" Width="100" Height="100" />

<Ellipse Fill="Black" Width="10" Height="15" Canvas.Left="28" Canvas.Top="28" />

<Ellipse Fill="Black" Width="10" Height="15" Canvas.Left="62" Canvas.Top="28" />

<Path Stroke="Black" StrokeThickness="6" Data="M 30,60 Q 50,90 70,60" /></Canvas>

Page 16: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

16 Introduction to WPF Noea / PQC - 2008Web

Shapes and Code

• Shapes accessible from code behind– Just like any element

• Change appearance by setting properties– Screen is automatically updated

<Ellipse Fill="Yellow" x:Name="myEllipse"

StrokeThickness="7" Stroke="Black" Width="100" Height="100" />

// ...in code behind

myEllipse.Width = 200;

Page 17: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

17 Introduction to WPF Noea / PQC - 2008Web

Transforms

• Any element can be transformed– Scaling, rotation, shearing

• Optionally affects layout

Page 18: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

18 Introduction to WPF Noea / PQC - 2008Web

Hit Testing

• Built in for all drawing elements

• Takes transformations into account

• Bubbling event model

Page 19: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

19 Introduction to WPF Noea / PQC - 2008Web

Brushes

• Specifies how shape is filled– Used for properties throughout WPF

• Polymorphic• Composable

<Rectangle Fill="Red" Width="200" Height="100" />

Page 20: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

20 Introduction to WPF Noea / PQC - 2008Web

Linear/RadialGradientBrush

• Fills across a range of colors• Enables interesting visual effects

Page 21: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

21 Introduction to WPF Noea / PQC - 2008Web

ImageBrush

<TextBlock FontSize="72" FontFamily="Arial Black"> <TextBlock.Foreground>

<ImageBrush ImageSource="c:\Windows\Blue Lace 16.bmp" />

</TextBlock.Foreground> Hello, World!</TextBlock>

Page 22: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

22 Introduction to WPF Noea / PQC - 2008Web

Composability: DrawingBrush

• Fill with vector image– Scalable

Page 23: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

23 Introduction to WPF Noea / PQC - 2008Web

Composability: VisualBrush

• Fill with any UI element• Makes certain design tricks easy

– Reflection of UI– Use as 3D surface texture

Page 24: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

24 Introduction to WPF Noea / PQC - 2008Web

OpacityMask

• Apply opacity to any visual using any Brush

Page 25: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

25 Introduction to WPF Noea / PQC - 2008Web

Imaging and Video

• Elements– Image– MediaElement

• Integrate images and video into a brush– Paint shapes– Use as a 3D surface texture

• Extensive image handling support– System.Windows.Media.Imaging

Page 26: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

26 Introduction to WPF Noea / PQC - 2008Web

Future Proofing the Platform and Your Applications

• Resolution independent graphics

• Double precision floating point coordinates and transformations

• Hardware capabilities are abstracted to better map to future hardware advances

Page 27: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

27 Introduction to WPF Noea / PQC - 2008Web

Summary

• Modern integrated content platform

• Consistency across UI and development

• Higher quality through hardware advances

Page 28: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

Controls and Layout

Page 29: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

29 Introduction to WPF Noea / PQC - 2008Web

Class Hierarchy

Page 30: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

30 Introduction to WPF Noea / PQC - 2008Web

Layout Controls

• StackPanel• WrapPanel• Canvas• DockPanel• Grid• ...

Page 31: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

31 Introduction to WPF Noea / PQC - 2008Web

Simple Controls

• PasswordBox• ScrollBar• ProgressBar• Slider• TextBox• RichTextBox• ...

Page 32: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

32 Introduction to WPF Noea / PQC - 2008Web

Content Controls

• Button• RepeatButton• ToggleButton• CheckBox• RadioButton• Label• Frame• ListBoxItem• StatusBarItem• ScollBarViewer

• ToolTip• UserControl• Window• NavigationWindow• ...

Page 33: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

33 Introduction to WPF Noea / PQC - 2008Web

Headered Content Controls

• Expander• GroupBoxItem• TabItem• ...

Page 34: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

34 Introduction to WPF Noea / PQC - 2008Web

Items Controls

• Menu• ContextMenu• StatusBar• TreeView• ListBox• ComboBox• TabControl• ...

Page 35: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

35 Introduction to WPF Noea / PQC - 2008Web

Summary

• Controls• Containers• Events• Commands

Page 36: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

Styles, Templates and Resources

Page 37: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

37 Introduction to WPF Noea / PQC - 2008Web

Designer Developer

Page 38: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

38 Introduction to WPF Noea / PQC - 2008Web

Skele

ton

Prototype

&

Experiment

Feedback from Customers

Fin

al

Pro

du

ct

Design

Development Process

Styling & Styling & TemplatesTemplates

Page 39: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

39 Introduction to WPF Noea / PQC - 2008Web

Styles are about setting properties…

Page 40: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

40 Introduction to WPF Noea / PQC - 2008Web

Setting Setting PropertiesProperties

Trigger Trigger

AnimationsAnimations

TemplateTemplate

StylesStyles

Page 41: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

41 Introduction to WPF Noea / PQC - 2008Web

Lookless Controls and Templates

• Control implies behaviour

• Probably supplies default look– Designer free to supply new look

Page 42: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

42 Introduction to WPF Noea / PQC - 2008Web

Page 43: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

43 Introduction to WPF Noea / PQC - 2008Web

Styling and Resources

• Style rarely defined inline– Tend to be reused for consistency

• Usually defined as resources

Page 44: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

44 Introduction to WPF Noea / PQC - 2008Web

Resource Dictionaries

• Simple Key, Value collection• FrameworkElement.Resources

<Grid> <Grid.Resources> <Style x:Key="dapper"> <Setter Property="Background" Value="LimeGreen" /> </Style> </Grid.Resources>

...

<Button Style="{StaticResource dapper}">Click</Button>

Page 45: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

45 Introduction to WPF Noea / PQC - 2008Web

Resource Hierarchy

System Resources

Application Resources

ElementResources

ElementResources

ElementResources

Window/PageResources

Window/PageResources

ElementResources

Application Resources

<Window> <Window.Resources> ... </Window.Resources>

<Grid> <Grid.Resources> ... </Grid.Resources>

<ListBox> <ListBox.Resources> ... </ListBox.Resources> </ListBox> </Grid></Window>

Page 46: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

46 Introduction to WPF Noea / PQC - 2008Web

Application Resources

• Styles and templates• Shared graphics

<Application x:Class="ResourcePlay.MyApp" xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005">

<Application.Resources> <Style x:Key="dapper"> <Setter Property="Background" Value="LimeGreen" /> </Style> </Application.Resources></Application>

Page 47: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

47 Introduction to WPF Noea / PQC - 2008Web

Page/Window Resources

• Utility elements– Data sources– Converters

• Page-specific templates

Page 48: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

48 Introduction to WPF Noea / PQC - 2008Web

Element-Specific Resources

• Locally-scoped Styles and templates

<Window ...> ... <Grid> <Grid.Resources> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Margin" Value="5" /> </Style> </Grid.Resources>

... </Grid> ...</Window>

Page 49: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

49 Introduction to WPF Noea / PQC - 2008Web

Automatic Styles/Templates

• Most resources must use x:Key• Optional with Styles and Data Templates

– Can use TargetType or DataType instead– Applied to target automatically – no reference required

<Window.Resources> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Margin" Value="5" /> </Style></Window.Resources>

Page 50: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

50 Introduction to WPF Noea / PQC - 2008Web

System Resources

• SystemColors – Colors and Brushes• SystemFonts• SystemParameters (sizes, settings)

<Rectangle Height="50" Width="100" Fill="{x:Static SystemColors.ControlBrush}" />

<!-- Better – responds to config changes --><Rectangle Height="50" Width="100" Fill="{DynamicResource {x:Static

SystemColors.ControlBrushKey}}" />

Page 51: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

51 Introduction to WPF Noea / PQC - 2008Web

Resources and Code-Behind

• FindResource• SetResourceReference• Change resource value with indexer

– someElem.Resources["foo"] = bar;• Initializing from code-behind not so good

– Must set before referencing XAML loaded– OK for app resources, less good otherwise

Page 52: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

52 Introduction to WPF Noea / PQC - 2008Web

Summary

• Styles empower designers• Styles enable vibrant visuals• Styles are the glue for WPF• Styles depend on resources

Page 53: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

53 Introduction to WPF Noea / PQC - 2008Web

Data Binding

Page 54: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

54 Introduction to WPF Noea / PQC - 2008Web

Agenda

• Element data binding

• Object and XML data binding

• Data templates

• Master / child relationships

• Debugging

Page 55: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

55 Introduction to WPF Noea / PQC - 2008Web

Data Binding Overview

• Target– Any property, any element

• Source– CLR Object– WPF Element– ADO.NET– XML

• Dynamic– INotifyPropertyChanged, DependencyProperty or

PropertyDescriptor• Multiple models

– One Time– One Way– Two Way

• Value Converter

• Target– Any property, any element

• Source– CLR Object– WPF Element– ADO.NET– XML

• Dynamic– INotifyPropertyChanged, DependencyProperty or

PropertyDescriptor• Multiple models

– One Time– One Way– Two Way

• Value Converter

Control

“Data Item” Property

Binding

Pro

perty

Page 56: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

56 Introduction to WPF Noea / PQC - 2008Web

Binding in Markup

<Image Source="truck.png"Canvas.Left= "{Binding Path=Value, ElementID=horzPos}"/>

<Slider Orientation= "Horizontal" Name="horzPos" Value="40"/>

{Binding Path=Value, ElementName=horzPos}

Page 57: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

57 Introduction to WPF Noea / PQC - 2008Web

Object Data Providers

• Add to resource dictionary– Named source objects

• Use with resource binding– {StaticResource theCars}

<Window> <Window.Resources> <ObjectDataProvider x:Key="myData" ObjectType=" {x:Type Foo}" /> </Window.Resources>

...

<TextBlock TextContent="{Binding Path=Bar, Source={StaticResource myData} }" />

Page 58: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

58 Introduction to WPF Noea / PQC - 2008Web

Share Common Source

StackPanel

Image

HorizontalSlider

Value= {Binding Path=XPos, Source={StaticResource myData}}

Canvas.Left= {Binding Path=XPos, Source={StaticResource myData}}

DataContext= {Binding Source={StaticResource myData}}

Value= {Binding Path=XPos}

Canvas.Left= {Binding Path=XPos}

Page 59: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

59 Introduction to WPF Noea / PQC - 2008Web

Provide Data From Code

• May be easier to load data in codebehind• Can set DataContext in code

Foo myDataObject = new Foo();myDataObject.Bar = 42;

// Set DataContextmyWindow.DataContext = myDataObject;

Page 60: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

60 Introduction to WPF Noea / PQC - 2008Web

Binding to XML

<Cars> <Car Make="Ford" Model="F-150"> <Image>truck.png</Image> </Car> <Car> ... </Car></Cars>

cars.xml

<XmlDataProvider x:Key="cars" XPath="/Cars/Car" Source="cars.xml" />

<TextBlock TextContent="{Binding XPath=@Make, Source={StaticResource cars}}"/>

Page 61: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

61 Introduction to WPF Noea / PQC - 2008Web

Provide XML From Code

• Can load XML data in codebehind– Just like any other data

XmlDocument doc = new XmlDocument();doc.LoadXml("<Foo><Bar>Hello, world</Bar></Foo>");

myGrid.DataContext = doc;

Page 62: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

62 Introduction to WPF Noea / PQC - 2008Web

Databases and Web Services

• No special support required

• Use object binding– DataSet/DataTable support the TypeDescriptor system

Page 63: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

63 Introduction to WPF Noea / PQC - 2008Web

Data and Controls

• ContentControl – singular content– Button, CheckBox, Label, ListBoxItem, …

• ItemsControl – plural content– ListBox, ComboBox, Menu, TabControl, ToolBar, …

• Can use data objects as content

myListBox.Items.Add(new Car());

myButton.Content = new Car();

Car c = (Car) myListBox.SelectedItem;

Page 64: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

64 Introduction to WPF Noea / PQC - 2008Web

Data Templates

DataTemplate

class Car{ string Image {get;set} string Model {get;set}}

<DataTemplate x:Key="carTemplate">

<Border BorderBrush="Blue" BorderThickness="2" Background="LightGray"

Margin="10" Padding="15,15,15,5">

<StackPanel>

<Image HorizontalAlignment="Center" Source="{Binding Path=Image}" />

<Border HorizontalAlignment="Center" BorderBrush="Navy"

Background="#DDF" BorderThickness="1" Margin="10" Padding="3">

<TextBlock FontSize="18" Text="{Binding Path=Model}" />

</Border>

</StackPanel>

</Border>

</DataTemplate>

Page 65: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

65 Introduction to WPF Noea / PQC - 2008Web

Choosing a Data Template

• By Name (resource dictionary key)– ContentTemplate/ItemTemplate= {StaticResource myStyle}

• By Type– <DataTemplate DataType="{x:Type cc:Car}">– <DataTemplate DataType="Book"> for XML

Page 66: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

66 Introduction to WPF Noea / PQC - 2008Web

Summary

• WPF understands your data– use your favorite data model: XML, objects, SQL, WS …– Bind data to controls

• Templates give your data a face

• Controls “think” data – Data is pervasive across and integrated into the platform

Page 67: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

Animation

Page 68: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

68 Introduction to WPF Noea / PQC - 2008Web

Why Animation?

• Enabling richer experiences

• Make a UI feel more naturalistic

• Smoother visual transitions

Page 69: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

69 Introduction to WPF Noea / PQC - 2008Web

Animation Anywhere

• Any property can be animated

• Anywhere ≠ everywhere

– Exercise taste & discretion

Page 70: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

70 Introduction to WPF Noea / PQC - 2008Web

Declarative animation

• Tell animation system what you want– E.g. change Width from 10 to 100 over 10 seconds

• Does all the work for you– No need to set up timers– No need for custom drawing

<DoubleAnimation From="10" To="100" Duration="0:0:10" Storyboard.TargetName="myellipse" Storyboard.TargetProperty="(Ellipse.Width)" />

Page 71: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

71 Introduction to WPF Noea / PQC - 2008Web

Essential Elements

• Triggers

• Timelines

• Animations

Page 72: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

72 Introduction to WPF Noea / PQC - 2008Web

Triggers

• Fire animations in response to stimuli– Use in element, Style, or template

• Two trigger styles:– Event-driven: <EventTrigger>– Property-driven: <Trigger> (enter and exit animations)

Page 73: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

73 Introduction to WPF Noea / PQC - 2008Web

Timelines

• A stretch of time: BeginTime and Duration• Timelines form a hierarchy• Some timelines are just structural

– E.g. ParallelTimeline• Other timelines do interesting things

– Animation timelines

Page 74: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

74 Introduction to WPF Noea / PQC - 2008Web

Animation Timelines

• Animations specific to target type– DoubleAnimation, PointAnimation, etc.

<DoubleAnimation From="10" To="100" Duration="0:0:10" Storyboard.TargetName="myellipse" Storyboard.TargetProperty="(Ellipse.Width)" />

Page 75: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

75 Introduction to WPF Noea / PQC - 2008Web

Relative vs Absolute Animations

• From and To• Just To• By• From and By

Page 76: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

76 Introduction to WPF Noea / PQC - 2008Web

Acceleration/Deceleration

• Adds more natural feel to motion

<DoubleAnimation From="50" To="70" AccelerationRatio="0.2" DecelerationRatio="0.3" Storyboard.TargetName="someElement" Storyboard.TargetProperty="(Ellipse.Width)" />

Sum must be <= 1

Page 77: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

77 Introduction to WPF Noea / PQC - 2008Web

Repeating and Reversing Timelines

• RepeatBehavior– Forever– Time, e.g. “0:0:5” repeats for 5 seconds– Count, e.g. “5.6x” repeats 5.6 times

• AutoReverse– True or False

Page 78: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

78 Introduction to WPF Noea / PQC - 2008Web

Key Frame Animations

• Simpler than lots of individual timelines• Also offer spline animation

Page 79: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

79 Introduction to WPF Noea / PQC - 2008Web

Animations and Styles

• Can add animation by restyling control– No special support required by control

• Style.Triggers

• ControlTemplate.Triggers

Page 80: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

80 Introduction to WPF Noea / PQC - 2008Web

Example: Triggers<ControlTemplate TargetType="{x:Type Button}">

<Grid>

<Rectangle Fill="{TemplateBinding Background}"

RadiusX="10" RadiusY="10" Stroke="Black" x:Name="outline" />

<ContentPresenter

HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"

VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />

</Grid>

<ControlTemplate.Triggers>

<EventTrigger RoutedEvent="Mouse.MouseEnter">

<EventTrigger.Actions>

<BeginStoryboard>

<Storyboard>

<DoubleAnimation Duration="0:0:0.2" AutoReverse="True"

Storyboard.TargetProperty="(Rectangle.StrokeThickness)"

Storyboard.TargetName="outline" By="1" />

</Storyboard>

</BeginStoryboard>

</EventTrigger.Actions>

</EventTrigger>

</ControlTemplate.Triggers>

</ControlTemplate>

Page 81: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

81 Introduction to WPF Noea / PQC - 2008Web

Animation in Code

• Can launch animations from code– Alternative to markup+storyboards– Allow more runtime flexibility

DoubleAnimation d = new DoubleAnimation(500, new Duration(TimeSpan.FromSeconds(2)));

myRect.BeginAnimation(Rectangle.WidthProperty,d);

Page 82: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

82 Introduction to WPF Noea / PQC - 2008Web

CompositionTarget.Render

• Animation timelines not always ideal solution– Real-time physics modeling doesn’t fit this model well

• Low level alternative: Render event– Called for each frame– That is all

• Note: frame rate not constant

Page 83: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

83 Introduction to WPF Noea / PQC - 2008Web

Summary

• Any property can be animated

• Define animations as Timeline hierarchies

• Put animations on page or in styles

• Declarative control with triggers

Page 84: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

84 Introduction to WPF Noea / PQC - 2008Web

Links

• Sites– http://windowsclient.net/ – http://wpf.netfx3.com/ (moving to windowsclient)– WPF Windows SDK Documentation– My Five Day Course For Hitting the WPF Curve/Cliff – www.brains-N-brawn.com/backRow/– http://sessions.visitmix.com/ – http://wpf.netfx3.com/files/folders/labs/default.aspx

(The tutorials are from here)– http://devlicio.us/blogs/rob_eisenberg/default.aspx

(Søg efter ‘Crash Course’

Page 85: 1 Introduction to WPFNoea / PQC - 2008Web Introduction to WPF Introduction to WPF Based on a Microsoft presentation.

85 Introduction to WPF Noea / PQC - 2008Web

Exercise

• Make tutorials of free choice from the tutorial folder or from the videos on http://windowsclient.net/

• The following are recommended:– Creating Rich 2D and 3D Content in WPF– ColorSwatch– Using Data Binding in WPF– LiveSearch (using a webservice and data binding)– The video tutorials!• You might also visit the video casts from the Mix

Conferences: http://sessions.visitmix.com/ • Challenge:– Make a rotating cube with a playing video on each side.– Might need some computing power to run :-(