windows presentation foudation - wpf day 3

23
By Apex TG India Pvt Ltd WPF, Windows Presentation Foundation A platform for building rich user experiences on Windows

Upload: apextgi

Post on 18-Jul-2016

29 views

Category:

Documents


5 download

DESCRIPTION

Windows Presentation Foundation (or WPF) is a graphical subsystem for rendering user interfaces in Windows-based applications by Microsoft. WPF, previously known as "Avalon", was initially released as part of .NET Framework 3.0. Rather than relying on the older GDI subsystem, WPF uses DirectX.

TRANSCRIPT

Page 1: Windows Presentation Foudation - WPF Day 3

ByApex TG India Pvt Ltd

WPF, Windows Presentation FoundationA platform for building rich user experiences on Windows

Page 2: Windows Presentation Foudation - WPF Day 3

Common Terms

DependencyProperty: Extend property functionality by providing a type that backs a property

DependencyObject: The base class that can register and own a dependency property

Dependency property identifier: A DependencyProperty instance, which is obtained as a return value when registering a dependency property, and then stored as a member of a class.

CLR "wrapper": The actual get and set implementations for the property

public static readonly DependencyProperty IsSpinningProperty = DependencyProperty.Register(... );public bool IsSpinning{ get { return (bool)GetValue(IsSpinningProperty); } set { SetValue(IsSpinningProperty, value); }}

Page 3: Windows Presentation Foudation - WPF Day 3

Setting Property Values

1. XAML attribute <Button Background="Red"

Content="Button!"/>2. Property element syntax.

<Button Content="Button!"> <Button.Background> <ImageBrush ImageSource="wavy.jpg"/> </Button.Background></Button>

3. Setting Properties in CodeButton myButton = new Button();myButton.Width = 200.0;

Page 4: Windows Presentation Foudation - WPF Day 3

Routed events

A type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event.

CLR event that is backed by an instance of the RoutedEvent class and is processed by the Windows Presentation Foundation (WPF) event system.

Routed events are events who navigate up or down the visual tree acording to their RoutingStrategy.

Top-level Scenarios for Routed Events Control composition and encapsulation:

Eg: Rich content model. Like an image inside of a Button. Singular handler attachment points:

To attach the same handler multiple times to process events that could be raised from multiple elements.

Example Code Class handling: Permit a static handler that is defined by the class.

(the opportunity to handle an event before any attached instance handlers can. Referencing an event without reflection:

Creates a RoutedEvent field as an identifier, which provides a robust event identification technique that does not require static or run-time reflection.

Page 5: Windows Presentation Foudation - WPF Day 3

How Routed Events Are Implemented

// Register the routed eventpublic static readonly RoutedEvent SelectedEvent =

EventManager.RegisterRoutedEvent( "Selected", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyCustomControl)); 

// .NET wrapper public event RoutedEventHandler Selected{ add

{ AddHandler(SelectedEvent, value); } remove { RemoveHandler(SelectedEvent, value); }} 

// Raise the routed event "selected“ RaiseEvent(new

RoutedEventArgs(MyCustomControl.SelectedEvent));

Page 6: Windows Presentation Foudation - WPF Day 3

Routing Strategies:

Bubbling: Event handlers on the event source are invoked. Then routes to successive parent elements until reaching the element tree root. To report input or state changes from distinct controls or other UI elements.

Direct: Only the source element itself is given the opportunity to invoke handlers in

response. Analogous to the "routing" that Windows Forms uses for events. unlike a standard CLR event, direct routed events support class handling.

Tunneling: Event handlers at the element tree root are invoked. Then The routed event then travels a route through successive child elements

along the route, towards the node element that is the routed event source. Compositing for a control, such that events from composite parts can be

deliberately suppressed or replaced by events that are specific to the complete control.

Input events provided in WPF often come implemented as a tunneling/bubbling pair.

Tunneling events are also sometimes referred to as Preview events, because of a naming convention that is used for the pairs.

Page 7: Windows Presentation Foudation - WPF Day 3

Resource

StaticResource: A simple way to reuse commonly defined objects and values. Provides a value for a XAML property by substituting the value of an already

defined resource.Static resource lookup behavior

For the requested key within the resource dictionary defined by the element that sets the property.

Traverses the logical tree upward, to the parent element and its resource dictionary.

Application resourcesWhen:

No intention of changing the value of the resource after it is referenced the first time.

Not reevaluated based on runtime behaviors such as reloading a page. So some performance benefit.

For Value of property that is not on a DependencyObject or a Freezable. Compiled into a DLL, and packaged as part of the application or shared between

applications Creating a theme for a custom control, and are defining resources that are used

within the themes.

Page 8: Windows Presentation Foudation - WPF Day 3

DynamicResource

Provides a value for a XAML property by deferring that value to be a run-time reference to a resource.

When: The value of the resource depends on conditions that are not known

until runtime. Like SystemColors, SystemFonts, or SystemParameters Creating or referencing theme styles for a custom control. To adjust the contents of a ResourceDictionary during an application

lifetime. A forward reference may be required The resource might not be used immediately when the page loads. (Static resource references always load from XAML when the page

loads; however, a dynamic resource reference does not load until it is actually used.) Applying resources to elements that might be reparented in the logical

tree during application lifetime.

Page 9: Windows Presentation Foudation - WPF Day 3

Dynamic resource

Dynamic resource lookup behavior Within the resource dictionary defined by the element that sets

the property. If the element defines a Style property, the Resources dictionary

within the Style is checked. If the element defines a Template property, the Resources

dictionary within the FrameworkTemplate is checked. Traverses the logical tree upward, to the parent element and its

resource dictionary. Application resources Theme resource dictionary System resources

Merged Resource Dictionary A way to define the resources portion of a WPF application

outside of the compiled XAML application. (Resources can then be shared across applications)

Page 10: Windows Presentation Foudation - WPF Day 3

Data Binding Overview

The process that establishes a connection between the application UI and business logic.

Elements can be bound to data from a variety of data sources in the form of common language runtime (CLR) objects and XML.

Features: ContentControls such as Button and ItemsControls such as ListBox

and ListView have built-in functionality to enable flexible styling of single data items or collections of data items.

Sort, filter, and group views can be generated on top of the data. Basic Data Binding Concepts

Page 11: Windows Presentation Foudation - WPF Day 3

Data Binding Overview

Direction of the Data Flow

What Triggers Source Updates

Page 12: Windows Presentation Foudation - WPF Day 3

Animation Overview

Animation is an illusion that is created by quickly cycling through a series of images, each slightly different from the last.

WPF includes an efficient timing system that is exposed through managed code and Extensible Application Markup Language (XAML) and that is deeply integrated into the WPF framework.

For a property to have animation capabilities, it must meet the following three requirements:

It must be a dependency property. It must belong to a class that inherits from

DependencyObject and implements the IAnimatable interface.

There must be a compatible animation type available. Controls such as Button and TabControl, and also Panel

and Shape objects inherit from DependencyObject.

Page 13: Windows Presentation Foudation - WPF Day 3

Storyboards Overview

A Storyboard is a type of container timeline that provides targeting information for the timelines it contains.

A Storyboard can contain any type of Timeline, including other container timelines and animations.

Storyboard objects enable you to combine timelines that affect a variety of objects and properties into a single timeline tree, making it easy to organize and control complex timing behaviors.

How to Apply Animations with a Storyboard To use a Storyboard to organize and apply animations, you add

the animations as child timelines of the Storyboard. The Storyboard class provides the Storyboard.TargetName and Storyboard.TargetProperty attached properties. You set these properties on an animation to specify its target object and property.

Sample Code

Page 14: Windows Presentation Foudation - WPF Day 3

Triggers Overview

Represents a trigger that applies property values or performs actions conditionally.Example:

WPF defines properties that correspond to end-user actions, such as the IsMouseOver property that is set to true when the user hovers the cursor over a UIElement or the corresponding IsMouseOver property of a ContentElement. Representing end-user actions in property values, along with the Trigger element, allows WPF styles to change property values based on those end-user actions, all from within markup.Sample Code

Page 15: Windows Presentation Foudation - WPF Day 3

Styles and Templates Overview

Styling and templating refer to a suite of features (styles, templates, triggers, and storyboards) that allow an application, document, or user interface (UI) designer to create visually compelling applications and to standardize on a particular look for their product.

Page 16: Windows Presentation Foudation - WPF Day 3

Trees in WPF

The Logical Tree: Why: Content models can readily iterate over their possible child

elements, and so that content models can be extensible. A framework for certain notifications, such as when all elements

in the logical tree are loaded. Resource references are resolved by looking upwards through

the logical tree for Resources collections on the initial requesting element and then parent elements.

Page 17: Windows Presentation Foudation - WPF Day 3

Trees in WPF

The Visual TreeThe structure of visuals represented by the Visual

base class.When you write a template for a control, you are

defining or redefining the visual tree that applies for that control.

That event routes for a routed event mostly travel along the visual tree, not the logical tree.

For Lower-level control over drawing for performance and optimization reasons

Sample Code

Page 18: Windows Presentation Foudation - WPF Day 3

Attached Events

A handler for a particular event to an arbitrary element rather than to an element that actually defines or inherits the event.

Sample Code:

Page 19: Windows Presentation Foudation - WPF Day 3

Input System

Subsystem provides a powerful API for obtaining input from a variety of devices, including the mouse, keyboard, and stylus.

Where:UIElement, ContentElement, FrameworkElement, and FrameworkContentElement Plus the Keyboard class and Mouse classes

For example: The key down event is associated with the KeyDown and

PreviewKeyDown events. Preview events tunneling down the element tree from

the root element to the target element. Bubbling events bubbling up from the target element to

the root element.

Page 20: Windows Presentation Foudation - WPF Day 3

Commanding Overview

Different from a simple event handler attached to a button or a timer that Commands separate the semantics and the originator of an action from its logic.

The semantics of the command are consistent across applications and classes, but the logic of the action is specific to the particular object acted upon.

Implement the ICommand. Example Code

Four Main Concepts in WPF Commanding The command: The action to be executed. The command source: The object which invokes the command. The command target: The object that the command is being executed on. The command binding: The object which maps the command logic to the command. The Paste command is the command, the MenuItem is the command source, the TextBox is

the command target, and the command binding is supplied by the TextBox control.

Commands By implementing the ICommand interface. ICommand exposes two methods, Execute, and CanExecute, and an

event, CanExecuteChanged. Similar to Routedevents, there are RoutedCommand 

Page 21: Windows Presentation Foudation - WPF Day 3

Commanding Overview

Command Sources: The object which invokes the command Examples: MenuItem, Button, and KeyGesture. Implement the ICommandSource interface. ICommandSource exposes three properties: Command,

CommandTarget, and CommandParameter.CommandBinding:

Associates a command with the event handlers that implement the command.

The CommandBinding class contains a Command property, and PreviewExecuted, Executed, PreviewCanExecute, and CanExecute events.

Example CodeCommand Library ApplicationCommands, NavigationCommands, MediaCommands,

EditingCommands, and the ComponentCommands.Creating Custom Commands Example Code

Page 22: Windows Presentation Foudation - WPF Day 3

References:

http://msdn.microsoft.com http://windowsclient.net http://www.wpftutorial.net http://joshsmithonwpf.wordpress.com http://idealprogrammer.com http://www.msdev.com http://www.wpfdev.com http://WPFpedia.com

Page 23: Windows Presentation Foudation - WPF Day 3