Intro To WPF
Intro To WPF
Intro To WPF
Agha.Azeem@usa.edu.pk
azeem2793@gmail.com
Credits: Telerik
Table of Contents
1. Windows Presentation Foundation (WPF)?
2. WPF basic features
3. WPF and other UI technologies
Windows Forms
DirectX
Silverlight
HTML + CSS
4. WPF classes
5. XAML basics
2
What is Windows
Presentation Foundation?
What is Windows
Presentation Foundation?
Windows Presentation Foundation (WPF)
11
Commands
Commands are more abstract and loosely-
coupled version of events
Examples: copy, cut, paste, save, etc.
WPF support for commands reduces the
amount of code we need to write
It gives us more flexibility
to change the UI
without breaking the back-end logic
Commands have action, source, target and
binding
12
Commands (2)
The power of commands comes from the
following three features
WPF defines a number of built-in commands
Commands have automatic support for input
actions
Some WPF controls have built-in behavior tied
to various commands
Commands are intended to do two things:
15
WPF and Other UI
Technologies
Windows Forms
Windows Forms is the culmination of the
display technology built on GDI+ and User32
It’s more mature than WPF
Has solid limitations
No separation between UI and the C# code
WPF is the platform for the future of Windows
development
Based on DirectX and Direct3D
Separation between the UI and C# code
17
DirectX
WPF create multimedia applications with real-
time graphics
Such as complex physics-based simulators or
cutting-edge action games
WPF applications are DirectX applications
As a result, even the most mundane business
applications can use rich effects and antialiasing
WPF graphics is entirely vector-based
This allows zoom-in / soom-out with no loss of
quality
18
Silverlight
Silverlight
is a cross-platform, cross-browser
plugin, which contains WPF-based technology
Rich Internet Application (RIA) platform
Including XAML and subset of WPF
Provides rich-media features such as video,
vector graphics, and animations
Silverlight
and WPF share the XAML
presentation layer
Both technologies are similar, but Silverlight is
limited in many aspects
19
HTML + CSS vs. XAML + Styles
Cascading Style Sheets (CSS) is a stylesheet
language
Used to describe the presentation of
information in HTML documents
XAML elements have Style property
Similar to CSS (as a concept)
The HTML and XAML are tag based systems to
describe UI
XAML is more powerful in many aspects
20
XAML Basics
XAML Basics
XAML is an XML-based language for creating
and initializing .NET objects
It’s used in WPF as a human-authorable way of
describing the UI
Used to separate the UI from the C# code
XAML contains a hierarchy of elements
representing visual objects
These objects are known as user interface
elements or UI elements
22
Elements and Attributes
UI elements have a set of common properties
and functions
Such as Width, Height, Cursor, and Tag
properties
Declaring an XML element in XAML
24
Example – Explanation
The root element, Window is used to declare a
portion of a class
The two XML namespace declarations pull in
two commonly used namespaces
The one for XAML itself
The one for WPF
Width, Height, Title are the XAML
properties
25
XAML Syntax
Live Demo
What is XAML?
What is XAML?
eXtensible Application Markup Language
XAML is a declarative markup language
29
Declarative UI with XAML
XAML introduces a new XML-based language
for describing UI elements known as XAML
XAML = eXtensible Application Markup
Language
XAML is a completely declarative language
A declarative language says "what"
An imperative language says "how"
XAML describes the behavior and integration
of components (in most cases UI components)
30
Property Elements
Not all properties have just a string value
Some must be set to an instance of an object
XAML provide syntax for setting complex
property values, called property elements
Take the form TypeName.PropertyName
contained inside an TypeName element
A property element
<Ellipse>
<Ellipse.RenderTransform>
<RotateTransform Angle="45" CenterY="60" />
</Ellipse.RenderTransform>
</Ellipse>
32
Property Elements
Live Demo
Declarative vs.
Programmatically?
Why we need XAML?
Declarative vs.
Programmatically
In WPF/Silverlighteach element can be done
either declaratively or programmatically
No difference in the execution and speed
Instantiating element from the code behind ruins
the idea of XAML
The same as Windows Forms
The following two examples are identical
With XAML From Code Behind