HO CHI MINH UNIVERSITY OF INDUSTRYWPF Declarative programming with XAML markup For Designers and Developers Rewritten from scratch – Built on top of Direct3D – Hardware accelerated
Trang 1What's New in WPF Version 4.5?
Trang 2HO CHI MINH UNIVERSITY OF INDUSTRY
Windows Presentation Foundation
Trang 3What is WPF?
WPF is a new API for building Windows applications
The goal of Windows Presentation Foundation (WPF) is to provide these advances for Windows Included in version 4.0 of the Microsoft NET Framework, WPF allows building interfaces that incorporate documents, media, two- and three-dimensional graphics, animations, Web-like characteristics, and much more
Trang 4HO CHI MINH UNIVERSITY OF INDUSTRY
What Windows Presentation Foundation Provides?
Three most important:
a unified platform for modern user interfaces
the ability for developers and designers to work together,
and a common technology for Windows and Web browser user interfaces
Trang 5What is WPF good for?
To enable designers and developers to work together
To allow an easy way to customize the look of controls without changing its behavior
To allow 3D graphics more easily in Windows applications
To allow an easy way to do animations in Windows applications
To enable the creation of applications which scale nicely to high resolution screens
Trang 6HO CHI MINH UNIVERSITY OF INDUSTRY
Which of these UI have you worked with?
GDI (20 years), GDI+, WinForms
DirectX (11 years), Direct3D
Quartz, DirectShow (8 years)
– Problems:
• Showing their age
• Each API is different
• Mixing APIs is challenging
Trang 7Next Gen
WPF – replaces GDI
Direct3D – large games, used by WPF
Media Foundation – ultimately will replace DirectShow
MCML –markup language for Media Center Edition applications
XNA – small games
Trang 8HO CHI MINH UNIVERSITY OF INDUSTRY
WPF
Declarative programming with XAML markup
For Designers and Developers
Rewritten from scratch
– Built on top of Direct3D
– Hardware accelerated
– Vector based
– Resolution independent (1/96 inch)
– Retained graphics
Trang 10HO CHI MINH UNIVERSITY OF INDUSTRY
Managed Code:
Code write in net framework environment is call Managed code, they will be executed direct from CLR(Common Language Runtime) CLR will manage memory, security problem, debugging…and other useful features
Trang 11Unmanaged Code
Not in net framework, that is not controlled by CLR, Unmanaged Code will
bee execute with 2 wrapper class support:
CCW (COM Callable Wrapper)
and
RCW (Runtime Callable Wrapper)
Trang 12HO CHI MINH UNIVERSITY OF INDUSTRY
CCW (COM Callable Wrapper)
Trang 13RCW (Runtime Callable Wrapper)
Trang 14HO CHI MINH UNIVERSITY OF INDUSTRY
Declarative object instantiation
Not exclusive to WPF
Separates UI and logic
– Common language for Designers and Developers
• Demo: Blend and VS – Parallel development
Trang 15 Markup/code-behind model (like ASP.NET)
Excellent layout options and text flow features
Access to powerful graphics hardware
Certain otherwise-impossible effects are made easy (skewing and rotating
textboxes, etc.)
Trang 16HO CHI MINH UNIVERSITY OF INDUSTRY
Tools
• Cider
• XAML Pad
• 3D tools such as Light wave can generate XAML
– Electric Rain Zam 3D
Trang 17Comparisons: WPF vs DirectX and GDI+
Trang 18HO CHI MINH UNIVERSITY OF INDUSTRY
WPF Build Pipeline
Trang 19Basic topics in WPF
WPF code and XAML
Property & object in XAML: syntax
Layout
Basic control
Basic property
WPF Concepts
Trang 20HO CHI MINH UNIVERSITY OF INDUSTRY
1 WPF code and XAML: first app
– New consoles project (or Windows Apps)
– Reference: PresentationCore, PresentationFramework, and WindowsBase
How to create this window?
Trang 21[ STAThread ]//Must insert before Main
public static void Main( string [] aargs){
Window w = new Window ();
btn.Width = btn.Height = 100;
w.Content = btn;
A.Run(w);}
}
}
Trang 22HO CHI MINH UNIVERSITY OF INDUSTRY
1 WPF code and XAML: first app
From VS template
App is inherited from Application
Window1 is inherited from Window
Trang 241 WPF code and XAML: first app
From VS template Write 2 example: code and Xaml
Trang 251 WPF code and XAML: first app
From VS template Write 2 example: code and Xaml
Trang 26HO CHI MINH UNIVERSITY OF INDUSTRY
2 Basic Window properties
Trang 30HO CHI MINH UNIVERSITY OF INDUSTRY
SolidColorBrush
XAML
Trang 31GradientBrush
Trang 32HO CHI MINH UNIVERSITY OF INDUSTRY
LinearGradientBrush
LinearGradientBrush
rect1.Fill = br;
Trang 34HO CHI MINH UNIVERSITY OF INDUSTRY
LinearGradientBrush
XAML
Trang 35RadialGradientBrush br = new
RadialGradientBrush(Colors.Blue,Colors.White);
Trang 36HO CHI MINH UNIVERSITY OF INDUSTRY
RadialGradientBrush
RadialGradientBrush br = new RadialGradientBrush ( Colors Blue, Colors Yellow);
rect1.Fill = br;
Trang 37RadialGradientBrush br = new RadialGradientBrush ();
Trang 38HO CHI MINH UNIVERSITY OF INDUSTRY
XAML
RadialGradientBrush
Trang 39ImageBrush myBrush = new ImageBrush ();
myBrush.ImageSource = new BitmapImage (
new Uri ( @"E:\Pictures\IC151762.jpg" , UriKind Relative));
rect1.Fill = myBrush;
ImageBrush
Trang 40HO CHI MINH UNIVERSITY OF INDUSTRY
Config Brush from VS design
Trang 412 Handling Application Events
From Code
Trang 42HO CHI MINH UNIVERSITY OF INDUSTRY
2 Handling Application Events
From XAML
Trang 432.Code-Behind and Object Names
<Button Name="myButton">Hello</Button>
Classes have a Name property
Classes don’t have a Name property
< Button x : Name ="myButton"> Hello </ Button >
Trang 44HO CHI MINH UNIVERSITY OF INDUSTRY
2 Logical Trees and Visual Trees
Logical tree comprises the elements as they are listed in the
XAML These include panels and controls you will generally use.
Visual tree includes the parts that make up the controls and panels.
Trang 462.Top-Level Elements
Three classes are used by WPF programs as top-level
elements
– Application – Page
– Window
• Only one Content
Trang 473 Syntax for Object
One element
Empty element tag
Trang 48HO CHI MINH UNIVERSITY OF INDUSTRY
3 Syntax for Object
Nested- Multi elements
Logical tree with four WFP
objects
XAML Tree With Four Elements
Trang 493 Syntax for Attribute
Object element syntax
– Like object syntax
• <button> FIT HUI </button>
Attribute syntax
Property Element Syntax
Attached Property Syntax
Trang 50HO CHI MINH UNIVERSITY OF INDUSTRY
3 Syntax for Attribute
Trang 513 Syntax for Attribute
Property Element Syntax
– Some properties are too complex to be set with a simple string
– Does not produce an object, sets a property on an object
Trang 52HO CHI MINH UNIVERSITY OF INDUSTRY
< Button >
< Button.Background >
< LinearGradientBrush StartPoint ="0,0" EndPoint ="1,1">
< GradientStop Color ="Red" Offset ="0.0"/>
< GradientStop Color ="Blue" Offset ="1.0"/>
</ LinearGradientBrush >
</ Button.Background >
Click me
</ Button >
3 Syntax for Attribute
Property Element Syntax
Trang 533 Syntax for Attribute
Attached Property Syntax
– Attached properties are a special type of property that is defined in one class but used in another
Trang 54HO CHI MINH UNIVERSITY OF INDUSTRY
3 Syntax for Attribute
Reviewing the XAML Syntax
Trang 55< StackPanel >
< StackPanel.Background >
< RadialGradientBrush >
< GradientStop Color="Black" Offset="0" />
< GradientStop Color="#FF56933A" Offset="1" />
< GradientStop Color="#FF2B2B2B" Offset="0.169" />
< TextBlock Grid.RowSpan="2" Height="23" />
< Button Content="Button" Grid.Row="1">
< Button.BorderBrush >
< LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
< GradientStop Color="Black" Offset="0" />
< GradientStop Color="#FFB44E4E" Offset="1" />
Trang 56HO CHI MINH UNIVERSITY OF INDUSTRY
5 XAML Namespace
To create objects of these types, the XAML parser must know where to find their definitions
Trang 575 XAML Namespace
Trang 58HO CHI MINH UNIVERSITY OF INDUSTRY
5 XAML Namespace: Classes from Other Namespaces
Trang 59Background = new LinearGradientBrush ( Colors Yellow, Colors Blue,
new Point (0,0), new Point (1,1));
}
}
} Create MyButton extend from class Button
Trang 60HO CHI MINH UNIVERSITY OF INDUSTRY
xmlns : local ="clr-namespace:StudyWPFApplication"
Declare prefix:
In XAML
< local : MyButton Content ="Hello" ></ local : MyButton >
Instantiate the button:
Trang 615 Markup Extension
Trang 62HO CHI MINH UNIVERSITY OF INDUSTRY
Trang 63 Layout in Windows From
Control have fix coordinates: X,Y
Dock, Anchor
Using FlowLayoutPanel to arrange controls in a flow panel
Using TableLaoutPanel to arrange controls in table format
6 Layout Basic Principle
Trang 64HO CHI MINH UNIVERSITY OF INDUSTRY
6 Layout Basic Principle
Flow-based layout
• Support coordinate
Contain is organizaed into containers
Resolution and size independent interfaces
Automatically adjust if screen resolution changes
Trang 66HO CHI MINH UNIVERSITY OF INDUSTRY
6 XAML Layout: Align & Padding
Alignment describes how child elements should be posisioned within
parent’s space
HorizontalAlignment: Left, Center, Right, or Stretch
VerticalAlignment: Top, Center, Bottom, or Stretch
Trang 67Margin
Distance between an elenment and its child or peers
Left, top, right, bottom, or uniform (Margin=“5”)
Padding
Enlarges a child element by specified thickness
6 XAML Layout: Align & Padding
Trang 68HO CHI MINH UNIVERSITY OF INDUSTRY
6 XAML Layout: Align & Padding
Trang 696 Layout Panels
Canvas
for specific (X,Y) positioning
Trang 70HO CHI MINH UNIVERSITY OF INDUSTRY
6 Layout Panels
StackPanel
Arranges content either horizontally or vertically
Trang 716 Layout Panels
WrapPanel
Arranges item sequentially
Trang 72HO CHI MINH UNIVERSITY OF INDUSTRY
6 Layout Panels
DockPanel
Anchor elements to the edges of the container
Trang 736 Layout Panels
Grid
Table-style layout of rows and columns
Trang 74HO CHI MINH UNIVERSITY OF INDUSTRY
6 Layout Panels
UniformGrid
All rows and columns are the same size
Contains two properties, Rows and Columns
Trang 76HO CHI MINH UNIVERSITY OF INDUSTRY
Types of Containers
Trang 77 Types of Containers : Example
Trang 78HO CHI MINH UNIVERSITY OF INDUSTRY
Trang 797.1 Border control
Container control can contains controls
Without border using Border control
Accept single child control
Display border around child control
Trang 80HO CHI MINH UNIVERSITY OF INDUSTRY
< Border Margin ="20,20,20,20" BorderBrush ="Blue" BorderThickness ="1,1,2,2"
Trang 81< RadioButton GroupName ="Office"
Content ="Microsoft Office 2010"
IsChecked ="True"/>
< RadioButton GroupName ="Office"
Content ="Open Office"/>
</ StackPanel >
RadioButton Properties
GroupName
IsChecked
Trang 82HO CHI MINH UNIVERSITY OF INDUSTRY
< CheckBox IsChecked ="True" Content ="Rất Hài
Lòng" Height ="21" Width ="145"></ CheckBox >
< CheckBox Content ="Hài Lòng" Height ="21" Width ="145"></ CheckBox >
< CheckBox Content ="Tạm Chấp Nhận" Height ="21" Width ="145"></ CheckBox >
< CheckBox Content ="Không Chấp Nhận" Height ="21" Width ="145"></ CheckBox >
</ StackPanel >
</ Border >
Trang 837.1 Basic Controls: Text, date, misc
TextBlock, Label, Textbox, PasswordBox
• View – edit text
TextBlock: Static text, standard font properties: FontFamily, FontSize, FontStyle,
FontWeight: for using simple static text
• <LineBreak/> to inject line break
• Using <Run> : each run have its own formatting
< TextBlock > FIT < LineBreak/ > HUI
< Run Background ="Blue" FontSize ="20"
Foreground ="red"> Hello </ Run > H.a.you?
</ TextBlock >
Trang 84HO CHI MINH UNIVERSITY OF INDUSTRY
7.1 Basic Controls: Text, date, misc
How to fill background for result text???
And How to change font style when mouse down???
Trang 857.1 Basic Controls: Label
When you want to display text by itself use the TextBlock The benefit is a
light, performant way to display text
When you want to associate text with another control like a TextBox use
the Label control The benefits are access keys and references to target
control
Trang 86HO CHI MINH UNIVERSITY OF INDUSTRY
7.1 Basic Controls: Label
<StackPanel>
<Label Target ="txta">Nhập _a</Label>
<TextBox Name ="txta"></TextBox>
<Label Target ="{Binding
ElementName =txtb, Mode =Default}">
Nhập _b</Label>
<TextBox Name ="txtb"></TextBox>
</StackPanel>
Trang 877.1 Basic Controls: Textbox control
Trang 88HO CHI MINH UNIVERSITY OF INDUSTRY
7.1 Basic Controls: Textbox control
Trang 897.1 Basic Controls: Passwordbox
Like Textbox
Property: PasswordChar
No Text property, rereive Password
Do not support clipboard
<PasswordBox Name="txtPassword" ToolTip="Password"
PasswordChar="*" Height="30" Width="224" />
Trang 90HO CHI MINH UNIVERSITY OF INDUSTRY
7.1 Date control: Calendar control
Tag <Calendar>
SelectionMode: select single day, range date
DiplayMode: Month, Year, Decade
BlackOutDates : set black out dates
DisplayDateStart, DisplayDateEnd: range of available dates
Trang 917.1 Date control: Calendar control
Trang 92HO CHI MINH UNIVERSITY OF INDUSTRY
7.1 Date control: DatePicker control
Allow to select a single date using dropdown calendar
IsDropDownOpen
SelectedDateFormat, BlackOutDates, DisplayDateStart, DisplayDateEnd
SelectedDateChanged event
Trang 937.1 Date control: DatePicker control
Trang 94HO CHI MINH UNIVERSITY OF INDUSTRY
Change Value, or using IsInDeterminate property
Check out System.Windows.Threading.DispatcherTimer to run code at regular intervals
• Demo
Image
Source
Stretch
Trang 957.1 Miscellaneous control
StackPanel >
<Slider Minimum ="0" Maximum ="10" Value ="5"></ Slider >
< ProgressBar Minimum ="0" Maximum ="100" Name ="prb"
< Image Source ="E:\Pictures\a.jpg" Height ="120.83"
< Button Click ="btn_Click" > Demo progressbar </ Button >
< Button Click ="btn1_Click" > Demo gressbar –
IsInDetermine </ Button >
</ StackPanel >
XAML
Trang 96HO CHI MINH UNIVERSITY OF INDUSTRY
7.1 Miscellaneous control
System.Windows.Threading DispatcherTimer timer;
private void btn_Click( object sender, RoutedEventArgs e)
{
timer= new System.Windows.Threading DispatcherTimer ();
timer.Tick += new EventHandler (my_tick);
timer.Interval = new TimeSpan (0, 0, 1);
Trang 977.1 Miscellaneous control
Example code 2
private void btn_Click
( object sender, RoutedEventArgs e) {
Trang 98HO CHI MINH UNIVERSITY OF INDUSTRY
7.1 Miscellaneous control
Display media
Souce property: URI
LoadedBehavior="Manual“ for using control methods
IsMuted
Position, Volume
Call Play, Pause, Stop methods
Invisible until playing video file
Trang 100HO CHI MINH UNIVERSITY OF INDUSTRY
<Button Click ="Play_Click" Margin ="3" Padding ="6,3">Play</Button>
<Button Click ="Pause_Click" Margin ="3" Padding ="6,3">Pause</Button>
<Button Click ="Stop_Click" Margin ="3" Padding ="6,3">Stop</Button>
</StackPanel>
</StackPanel>
7.1 Miscellaneous control
Example 2 - XAML