+ All Categories
Home > Documents > Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group...

Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group...

Date post: 03-Jan-2016
Category:
Upload: elfreda-nelson
View: 219 times
Download: 0 times
Share this document with a friend
59
Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich [email protected] http://blogs.msdn.com/knom/
Transcript
Page 1: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Windows Presentation FoundationWindows Presentation Foundation

Maximilian KnorDeveloper EvangelistDeveloper and Plattform GroupMicrosoft Österreich

[email protected] http://blogs.msdn.com/knom/

Page 2: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

MSDN Briefings – OrganisationMSDN Briefings – Organisation

Monthly technical briefingsCurrently released technologyYour current needs

Invitation / Registration / Feedbackhttp://blogs.msdn.com/msdnat http://blogs.msdn.com/talk

Well, what I am doing here?

Page 3: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

TopicsTopics

Windows Presentation Foundation

Overview2D

Controls and Layout

Styles, Templates & ResourcesData BindingAnimationInterop and Migration

Page 4: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Why WPF? Another UI Framework?Why WPF? Another UI Framework?

Aren‘t these some classicsApp flickers on re-sizeOverlay controls and videosThemeing und styling

What‘s the cause?Win32 has limitsGraphics rendered by CPU

Page 5: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

The WPF ApproachThe WPF Approach

Unify UI, Documents, MediaIntegrated development

New Windows infrastructure Vector based graphicsUse display hardware (D3D)Built-in UI patterns

Designers and toolsBringing designers into the processDeclarative programming models

Page 6: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

.NET 3.0.NET 3.0

Operating System

.NET Framework 2.0

.NET Framework 3.0

WindowsPresentationFoundation

WindowsCommunication

Foundation

WindowsWorkflow

Foundation

WindowsCard Space

WPFUser

Experience

WCFUnified

Messaging

WFFederated Workflow

CSFederated

Identity

Page 7: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

WPF Programming ModelWPF Programming Model

Button b1 = new Button();b1.Content = "OK";b1.Background = new SolidColorBrush(Colors.LightBlue);b1.Width = new Length(100);

<Button Width="100px"> OK <Button.Background> HorizontalGradient= "White LtBlue" </Button.Background></Button>

Dim b1 As New Buttonb1.Content = "OK"b1.Background = New SolidColorBrush(Colors.LightBlue)b1.Width = New Length(100)

Separate UI and behaviorDo you know ASP.NET Similar but more powerful

XAMLWhat, Not How!Creates object hierarchy

Page 8: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Designer Developer

Developer Designer CollaborationDeveloper Designer Collaboration

Page 9: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

XAMLXAML

Extensible Applications Markup Language

<Button Name="button1" Background="Red">

Click Me!</Button>

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

Colors.Red);

Page 10: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Properties as Attributes or ElementsProperties as Attributes or Elements

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

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

Page 11: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Attached PropertiesAttached Properties

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

Page 12: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Markup ExtensionsMarkup Extensions

<Button Name="button1"> <Button.Style> <StaticResource ResourceKey="key" /> </Button.Style> Click Me!</Button>

<Button Name="button1" Style="{StaticResource key}" Content="Click Me" />

Page 13: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

TopicsTopics

Windows Presentation Foundation

Overview2D

Controls and Layout

Styles, Templates & ResourcesData BindingAnimationInterop and Migration

Page 14: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

ShapesShapes

Elements of UI treeJust like controls and other elements

Rectangle

Ellipse

Polyline

Polygon

Path

Page 15: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

DemoDemo

2D Shapes

Page 16: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Shapes and CodeShapes and Code

Shapes accessible from code behindJust like any element

Change appearance by setting propertiesScreen is automatically updated

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

// ...in code behind

myEllipse.Width = 200;

Page 17: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

BrushesBrushes

Specifies how shape is filledUsed for properties throughout WPF

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

Page 18: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Linear/RadialGradientBrushLinear/RadialGradientBrush

Fills across a range of colorsEnables interesting visual effects

Page 19: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

ImageBrushImageBrush

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

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

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

Page 20: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Composability: DrawingBrushComposability: DrawingBrush

Fill with vector imageScalable

Page 21: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Composability: VisualBrushComposability: VisualBrush

Fill with any UI elementMakes certain design tricks easy

Reflection of UIUse as 3D surface texture

Page 22: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

OpacityMaskOpacityMask

Apply opacity to any visual using any Brush

Page 23: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

TransformationsTransformations

Any element can be transformedScaling, rotation, moving

Optionally affects layoutLayoutTransformRenderTransform

Included into hit testing

Page 24: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

DemoDemo

Transformations

Page 25: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

TopicsTopics

Windows Presentation Foundation

Overview2D

Controls and Layout

Styles, Templates & ResourcesData BindingAnimationInterop and Migration

Page 26: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Class HierarchyClass Hierarchy

Page 27: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Dependency PropertyDependency Property

public class MyDependencyObject : DependencyObject{ public static readonly DependencyProperty SomeStateProperty = DependencyProperty.Register("SomeState", typeof(String), typeof(MyDependencyObject));  public string SomeState { get { return (string)this.GetValue(SomeStateProperty); } set { this.SetValue(SomeStateProperty, value); } }}

Page 28: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Layout ControlsLayout Controls

StackPanelWrapPanelCanvasDockPanelGrid...

Page 29: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

WPF PositioningWPF Positioning

Absolute PositioningX / Y, Width / Height

Relative PositioningNO X/Y, Width/HeightMarginAlignment

<Button Margin="20,10,20,10">Hallo Welt</Button>

Page 30: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

WPF Positioning - AlignmentWPF Positioning - Alignment

HorizontalAlignmentStretch, Left, Right, Center

VerticalAlignmentStretch, Top, Center, Bottom

<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> Hallo Welt</Button>

<Button HorizontalAlignment="Left"

VerticalAlignment="Bottom">

Hallo Welt</Button>

Page 31: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Simple ControlsSimple Controls

PasswordBoxScrollBarProgressBarSliderTextBoxRichTextBox...

Page 32: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Content ControlsContent Controls

ButtonRepeatButtonToggleButtonCheckBoxRadioButtonLabelFrameListBoxItemStatusBarItemScollBarViewer

ToolTipUserControlWindowNavigationWindow...

Page 33: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

DemoDemo

Controls

Page 34: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

EventsEvents

Routed Event Handling

Routing StrategyTunneling, Bubbling, Direct

Page 35: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

EventsEvents

Create Routed Events

public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent("Tap", RoutingStrategy.Bubble,

typeof(RoutedEventHandler), typeof(MyButtonSimple));

public event RoutedEventHandler Tap { add { AddHandler(TapEvent, value); }remove { RemoveHandler(TapEvent, value); }

}

Page 36: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

EventsEvents

Event notationPreviewXxx – TunnelledXxx - Bubbled

Base Class RoutedEventArgs

Attached Events

Page 37: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

DemoDemo

Routed Event Handling

Page 38: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Headered Content ControlsHeadered Content Controls

• Content Control with additional header

• Expander

• MenuItem

• GroupBox

• TabItem

• Usw.

Page 39: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Items ControlsItems Controls

• Container for multiple items (list)

• ListBox• ComboBox

• Menu• ContextMenu• StatusBar

• TreeView• TabControl

• Usw.

Page 40: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

CommandsCommands

Controls define a command

CommandBindings define the handler

<Button Command="ApplicationCommands.New"> <Image Source="toolbargraphics/New.bmp" /> </Button>

<Window.CommandBindings> <CommandBinding Command="ApplicationCommands.New" Executed="FileNew" CanExecute="CanFileNew" /></Window.CommandBindings>

Page 41: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

DemoDemo

TabControl, Listbox, Toolbar, Commands

Page 42: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

TopicsTopics

Windows Presentation Foundation

Overview2D

Controls and Layout

Styles, Templates & ResourcesData BindingAnimationInterop and Migration

Page 43: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Designer

Developer

Page 44: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Ske

leto

nPrototype

&

Experiment

Feedback from Customers

Fin

al

Pro

du

ct

Design

Development Process

Styling & Templates

Page 45: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Styles are about setting properties…Styles are about setting properties…

Page 46: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Setting Properties

Trigger

Animations

Template

Styles

Page 47: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Lookless Controls and TemplatesLookless Controls and Templates

Control implies behaviour

Probably supplies default lookDesigner free to supply new look

Page 48: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com
Page 49: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Styling and ResourcesStyling and Resources

Style rarely defined inlineTend to be reused for consistency

Usually defined as resources

Page 50: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Resource DictionariesResource Dictionaries

Simple Key, Value collectionFrameworkElement.Resources

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

...

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

Page 51: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Resource HierarchyResource 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 52: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Application ResourcesApplication Resources

Styles and templatesShared 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 53: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Page/Window ResourcesPage/Window Resources

Utility elementsData sourcesConverters

Page-specific templates

Page 54: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Element-Specific ResourcesElement-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 55: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Automatic Styles/TemplatesAutomatic Styles/Templates

Most resources must use x:KeyOptional with Styles and Data Templates

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

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

Page 56: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

DemoDemo

Styles

Page 57: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

System ResourcesSystem Resources

SystemColors – Colors and BrushesSystemFontsSystemParameters (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 58: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Resources and Code-BehindResources and Code-Behind

FindResource

SetResourceReference

Change resource value with indexersomeElem.Resources["foo"] = bar;

Initializing from code-behind not so goodMust set before referencing XAML loadedOK for app resources, less good otherwise

Page 59: Windows Presentation Foundation Maximilian Knor Developer Evangelist Developer and Plattform Group Microsoft Österreich max.knor@microsoft.com

Recommended