Transcript Slide 1

Windows Presentation Foundation
The next generation of Windows Forms
Adam Calderon
Principal Engineer
Interknowlogy LLC
[email protected]
http://blogs.interknowlogy.com/adamcalderon
What is WPF
Windows Presentation Foundation is Microsoft's
unified presentation subsystem for Windows, and is
exposed through WinFX and XAML. It consists of a
display engine and a managed-code framework.
Windows Presentation Foundation unifies how
Windows creates, displays, and manipulates
documents, media, and user interface (UI), enabling
developers and designers to create visually stunning,
differentiated user experiences
What is WinFx
WinFX is Windows Vista's managed-code
programming model, building on and extending the
.NET Framework. WinFX offers both practical solutions
to today's software challenges and new opportunities
to create software and services not possible now. It
enables both developers and designers to quickly
create new applications and experiences that are
more reliable and secure, visually stunning, smarter
about information management, better connected, and
more collaborative.
What is XAML
XAML is a markup language that declaratively
represents user interfaces for Windows applications,
improving the richness of the tools with which
developers and designers can compose and
repurpose UI.
For Web developers, XAML provides a familiar UI
description paradigm. XAML also enables the
separation of UI design from the underlying code,
enabling developers and designers to work more
closely together.
Sample XAML
<Window x:Class="WindowsApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
Title="WindowsApplication1">
<Grid>
<Label
VerticalAlignment="Top"
HorizontalAlignment="Left“
Grid.Column="0"
Grid.ColumnSpan="1"
Grid.Row="0"
Grid.RowSpan="1“
Margin="32,37,0,0"
Width="115.63"
Height="23"
Name="label1">Hello World</Label>
</Grid>
</Window>
WPF Application Types
Windows Application
– Standard single window type
– Browser navigation type
Browser Application
– Runs in a sandbox with “Internet Permissions”
Layout Basics
Panels layout children
Size to content
Alignments
Margins
Padding
Content Overflow
Panels – How you layout forms
DockPanel
– Allocates the entire edge of the panel area to each
child
StackPanel
– Lays out children in a vertical or horizontal stack
Grid
– Arranges children within a grid
Canvas
– Performs no layout logic. Puts children where you tell
it to.
Controls
Events
Commands
Built-In Controls
Control Events (Routed Events)
Bubbling
– Starts at the current element and bubbles up to the
current element’s parent and that elements parent
and so on [MouseLeftButtonDown]
Tunneling
– Works opposite of bubbling and starts at the root
element and works down to the originating element
[PreviewMouseLeftButtonDown]
Direct
– Works like standard .NET event handling with only
direct handlers being notified of events [Click]
Working with Events
Halting Events
Private void Grid_ButtonDown(object sender, RoutedEventArgs e)
{
//do something here
e.Handled = true;
}
Determining the Target
Private void Grid_ButtonDown(object sender, RoutedEventArgs e)
{
// who is the originator
MessageBox.Show(((System.Windows.Controls.Control)e.OriginalSource).Name);
}
Commands
Logical action that can be invoke in
several ways
Based on the RoutedCommand Class
Uses both event tunneling
[PreviewExecuteEvent] and event
bubbling [ExecuteEvent]
Standard Command Classes
Class
Command Types
ApplicationCommands
Commands common to almost all
applications. Includes clipboard
commands, undo and redo and document
level operations (open,close,print,etc..)
ComponentCommands
Operations for moving through information
such as scroll up and down, move to end,
and text selection
EditCommands
Text editing commands such a bold, italic
and alignment
MediaCommands
Media-playing operations such as
transport (play,pause,etc..), volume
control and track selection
Built-In Controls
Not wrappers around old Win32 controls
Native WPF controls
Support styling, resolution independence, data
binding, composition and full
support for WPF graphic
capabilities
Includes Most Standard Controls
–
–
–
–
Buttons
Slider and Scroll
Text Controls (Textbox, Label)
Menus and Toolbars
Data Binding
Simple Field level Data Binding
Complex Data Binding to multiple controls
at a time (Master/Detail/Detail)
Client-Side Sorting and Filtering
Support for binding to Objects, XML and
Relational data sources
Simple Data Binding
Simple Data Binding using Business
Objects
Support for two-way synchronization in
simple objects via the
INotifyPropertyChanged interface
Implicit and Declarative binding via the
DataContext
Complex Data Binding
Binding to Lists
Supports two-way data binding to list if list
inherits from ObservableCollection<T>
Data Templates
Sorting
Filtering
Master/Detail/Detail
Multiple Data Sources
Object Data Source
XML Data Source
Relational Data Source
Styles
Used for both Elements (Controls) and
Data Templates
Can be used to set properties (Font) or
define the way an object looks
Different types: Inline, Named
Can target a specific type and can
inherited like CSS.
Laying out files with Styles in Mind
Window1.xaml.cs
Window1.xaml
Styles.xaml
Contains:
Application logic
Contains:
Principal UI
(controls, layout, etc.)
Contains:
Application “style”
(visual description of
UI elements)
Created by:
Application developer
Created by:
Application developer
or designer
Created by:
Application designer
Design Experience:
By hand or using a
visual tool
Design Experience:
By hand or using a
visual tool
Data model, event names,
element names, etc.
Resource names and
element types.
Triggers
Property Triggers
Data Triggers
Event Triggers
What else is in WPF
Control Templates
Resources
Graphics
Annimation
Custom Controls
Integration with VS2005 Controls
ClickOnce Deployment
Resources
Windows Vista Developers Center
http://msdn.microsoft.com/windowsvista/default.aspx
Windows Presentation Foundation Forum
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=119&SiteID=1
WinFx December CTP
http://msdn.microsoft.com/windowsvista/getthebeta/default.aspx
Programming Windows Presentation
Foundation (by Chris Sells and Ian Griffiths)
http://www.amazon.com/gp/product/0596101139/qid=11361
44280/sr=8-1/ref=pd_bbs_1/102-95842115272153?n=507846&s=books&v=glance