You are on page 1of 21

WPF, Windows Presentation Foundation

A platform for building rich user experiences on Windows

By
Apex TG India Pvt Ltd

Presentation Scope

What's covered:
XMAL
Basic Elements of WPF
Architecture
Property System / Dependency Property
Routed Events
Binding System
Styling / Templating

Whats Not covered:


Controls Library

Graphics / Multi Media


Documents

What is WPF?
Next-generation presentation system for building Windows client
applications with visually stunning user experiences.
Resolution-independent
Vector-based rendering engine (advantage of modern graphics
hardware)
Coverage:
Extensible Application Markup Language (XAML), controls, data
binding, layout, 2-D and 3-D graphics, animation, styles, templates,
documents, media, text, and typography.
Types Of Applications:
1. Standalone Applications:

Window class to create windows and dialog boxes


Accessed from menu bars and tool bars.

2. Browser-Hosted Applications:
(XAML browser applications (XBAPs))
Create pages(Page) and page functions (PageFunction(T))
Navigate between usinghyperlinks (Hyperlink classes).

Tech Analysis

WinForm
s

PDF

WinForm
s+
GDI

Windows
Media
Player

Direct3D

WPF

Forms,
Controls

Complex text

Images

Video / Audio

2D Graphics

3D Graphics

Reference: http://windowsclient.net/wpf/white-papers/when-to-adopt-wpf.aspx

The Application Class


Additional application-scoped services, including
Creating and managing common application infrastructure.
Tracking and interacting with application lifetime.
Startup
Activated
Deactivated
ShutdownMode
(OnLastWindowClose,OnMainWindowClose,
OnExplicitShutdown)
SessionEnding
(Logging off,Shutting down,Restarting,Hibernating)
Exit

Retrieving and processing command-line parameters.

Example
Sharing application-scope properties and resources.

Examples in Code
Detecting and responding to unhandled exceptions.
Example in Code

The Application Class

Returning exit codes.


Example in Code

Managing windows in standalone applications

Tracking and managing navigation


using System.Windows.Navigation;
1. <Hyperlink Control

<Hyperlink NavigateUri="UriOfPageToNavigateTo.xaml">
Navigate to Another Page
</Hyperlink>
2. Via NavigationService
// Instantiate the page to navigate to
PageWithNonDefaultConstructor page = new
PageWithNonDefaultConstructor("Hello!");
// Navigate to the page, using the NavigationService
this.NavigationService.Navigate(page);

The Application Class


3.Programmatic Navigation with a Pack URI
// Create a pack URI
Uri uri = new Uri("AnotherPage.xaml", UriKind.Relative);
// Get the navigation service that was used to
// navigate to this page, and navigate to
// AnotherPage.xaml
this.NavigationService.Navigate(uri);

Configuring the Host Window's Title, Width, and


Height
Fragment Navigation
Navigation to a content fragment
PageURI#ElementName

Navigation Lifetime
Navigating. Occurs when a new navigation is requested. Can be
used to cancel the navigation.
NavigationProgress. Occurs periodically during a download to
provide navigation progress information.
Navigated. Occurs when the page has been located and
downloaded.
NavigationStopped. Occurs when the navigation is stopped (by
calling StopLoading), or when a new navigation is requested while a
current navigation is in progress.
NavigationFailed. Occurs when an error is raised while navigating
to the requested content.
LoadCompleted. Occurs when content that was navigated to is
loaded and parsed, and has begun rendering.
FragmentNavigation Occurs when navigation to a content
fragment begins, which happens:
Immediately, if the desired fragment is in the current content.
After the source content has been loaded, if the desired fragment is in
different content.

Navigation Lifetime

Navigation Lifetime
Journal / journal entry(JournalEntry class)
The back stack, the forward stack
1. Declarative mechanisms provided by WPF
<Hyperlink Members of the NavigationService
classCommand="NavigationCommands.BrowseBack">Back</Hyper
link>
2. Programmatic mechanisms provided by WPF
GoBack
GoForward
CanGoBack
CanGoForward
Retaining Content State with Navigation History
New Page, so data is destroyed, But there is a mechanism to restore
state via Journal like navigation history.
Custom implementation is also provided
System.Windows.Navigation.CustomContentState

Navigation Lifetime
Structured Navigation Overview
Structured Navigation with PageFunction
Other Types of Structured Navigation

NavigationWindow Class
browser-style navigation into your standalone applications
Navigation Hosts

Configuring the Application Definition for MSBuild


In MSBuild project
<ApplicationDefinition Include="App.xaml" />
Getting the Current Application
// Get strongly-typed current application
App app = (App)App.Current;

Two way to Start Window or Page


1. ByCode
void App_Startup(object sender, StartupEventArgs e)
{
((NavigationWindow)this.MainWindow).Navigate(new Uri("HomePage.xaml",
UriKind.Relative));
}

2. StartupUri="MainWindow.xaml"

WPF Architecture

WPF Architecture
More declarative, "property centric" model of programming
System.Threading.DispatcherObject
Support for concurrency and threading.
messaging system implemented by the dispatcher.
Create a CLR object that has STA behavior.

Thread affinity:
A component uses the identity of the executing thread to store some type of
state.
OLE 2.0, the clipboard, and Internet Explorer all require single thread affinity
(STA) execution.

The dispatcher:
Basic message dispatching system, with multiple prioritized queues
Examples of messages include:

Raw input notifications (mouse moved)


Framework functions (layout)
User commands (execute this method).

WPF Architecture
System.Windows.DependencyObject
Dependency Properties:
To compute the value of a property based on the value
of other inputs
System properties such as themes and user preference
Just-in-time property determination mechanisms such as data
binding and animations/storyboards
Multiple-use templates such as resources and styles
Values known through parent-child relationships with other
elements in the element tree
Also For:
Self-contained validation
Default values
Callbacks that monitor changes to other properties
A system that can coerce property values based on potentially
runtime information.

WPF Architecture
System.Windows.Media.Visual
Building a tree of visual objects
Drawing instructions and metadata about how to render
those instructions
(clipping, transformation, etc.).
Point of connection between these two subsystems, the
managed API and the unmanaged milcore.

WPF Architecture
Composition:
In User32 and GDI:
(Immediate mode clipping system)
a clipping bounds outside of which the component isnt allowed
to touch the pixels, and then the component is asked to paint
pixels in that box.
Good for memory constrained environment.

In WPF:

"Painter's algorithm" painting model.


To render from the back to the front of the display.
Each component to paint over the previous component's display.
Complex, partially transparent shapes.
Better for new type of graphics object.

WPF Architecture

System.Windows.UIElement

Core subsystems including Layout, Input, and Events.

Layout:
1. A fixed set of layout models (HTML supports three models for layout; flow, absolute,
and tables)
2. No model for layout (User32 really only supports absolute positioning)
A flexible, extensible layout model, which could be driven by property values rather than
imperative logic.

A two phase model with Measure and Arrange passes.

Measure Phase: To determine how much size it would like to take.

A parent element will ask a child to measure several times to determine its optimal position and size.
Rule: Size to content.

Arrange phase: To allows a parent to position and determine the final size of each child.

Input:

On a kernel mode device driver.


Gets routed to the correct process and thread by involving the Windows kernel and User32.
Routed to WPF.
WPF raw input message and sent to the dispatcher.
Raw input events to be converted to multiple actual events.

WPF Architecture
Routing through the element tree.
Events are said to "bubble" if they traverse from a target up
the tree to the root.
Events are said to "tunnel" if that start at the root and
traverse down to a target.

In the dispatcher for your application you would call


TranslateAccelerator which would sniff the input
messages in User32 and determine if any matched a
registered accelerator.
CommandBindings.
To define functionality in terms of a command end point
something that implements ICommand.
Enable an element to define a mapping between an input
gesture (Ctrl+N) and a command (New).

WPF Architecture
System.Windows.FrameworkElement
A set of policies and customizations on the subsystems introduced in lower
layers of WPF.
A set of new subsystems

Policies:
Application layout
Direct access to animation through theBeginStoryboard method
The data binding subsystem: one or more properties from a given element
to be bound to a piece of data
Styling Subsystem: Bind a set of properties from a shared definition to one
or more instances of an element.

System.Windows.Controls.Control
Templating allows a control to describe its rendering in a parameterized,
declarative manner.
The implementation of a control provides a data model and interaction
model
This split between the data model (properties), interaction model
(commands and events), and display model (templates) enables complete
customization of a controls look and behavior.

WPF property system


A set of services that can be used to extendthe functionality of a common
language runtime (CLR) property.
Dependency property:
A property that is backed by the WPF property system.
What For?
1. A way to compute the value of a property based on the value of other
inputs
System properties such as themes and user preference
Just-in-time property determination mechanisms such as data binding and
animations/storyboards Multiple-use templates such as resources and styles
Values known through parent-child relationships with other elements in the
element tree.

2. To provide self-contained validation, default values, callbacks that


monitor changes to other properties
3. A system that can coerce property values based on potentially runtime
information.
4. Derived classes can also change some specific characteristics of an
existing property by overriding dependency property metadata, rather than
overriding the actual implementation of existing properties or creating new
properties.

You might also like