1. Trang chủ
  2. » Công Nghệ Thông Tin

Windows Presentation Foundation pot

164 3,1K 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Windows Presentation Foundation pot
Trường học Ho Chi Minh University of Industry
Thể loại Thiết án
Thành phố Ho Chi Minh City
Định dạng
Số trang 164
Dung lượng 6,71 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

What's New in WPF Version 4.5?

Trang 2

HO CHI MINH UNIVERSITY OF INDUSTRY

Windows Presentation Foundation

Trang 3

What 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 4

HO 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 5

What 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 6

HO 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 7

Next 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 8

HO 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 10

HO 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 11

Unmanaged 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 12

HO CHI MINH UNIVERSITY OF INDUSTRY

CCW (COM Callable Wrapper)

Trang 13

RCW (Runtime Callable Wrapper)

Trang 14

HO 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 16

HO CHI MINH UNIVERSITY OF INDUSTRY

Tools

• Cider

• XAML Pad

• 3D tools such as Light wave can generate XAML

– Electric Rain Zam 3D

Trang 17

Comparisons: WPF vs DirectX and GDI+

Trang 18

HO CHI MINH UNIVERSITY OF INDUSTRY

WPF Build Pipeline

Trang 19

Basic topics in WPF

 WPF code and XAML

 Property & object in XAML: syntax

 Layout

 Basic control

 Basic property

 WPF Concepts

Trang 20

HO 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 22

HO 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 24

1 WPF code and XAML: first app

From VS template Write 2 example: code and Xaml

Trang 25

1 WPF code and XAML: first app

From VS template Write 2 example: code and Xaml

Trang 26

HO CHI MINH UNIVERSITY OF INDUSTRY

2 Basic Window properties

Trang 30

HO CHI MINH UNIVERSITY OF INDUSTRY

SolidColorBrush

XAML

Trang 31

GradientBrush

Trang 32

HO CHI MINH UNIVERSITY OF INDUSTRY

LinearGradientBrush

LinearGradientBrush

rect1.Fill = br;

Trang 34

HO CHI MINH UNIVERSITY OF INDUSTRY

LinearGradientBrush

XAML

Trang 35

RadialGradientBrush br = new

RadialGradientBrush(Colors.Blue,Colors.White);

Trang 36

HO CHI MINH UNIVERSITY OF INDUSTRY

RadialGradientBrush

RadialGradientBrush br = new RadialGradientBrush ( Colors Blue, Colors Yellow);

rect1.Fill = br;

Trang 37

RadialGradientBrush br = new RadialGradientBrush ();

Trang 38

HO CHI MINH UNIVERSITY OF INDUSTRY

XAML

RadialGradientBrush

Trang 39

ImageBrush myBrush = new ImageBrush ();

myBrush.ImageSource = new BitmapImage (

new Uri ( @"E:\Pictures\IC151762.jpg" , UriKind Relative));

rect1.Fill = myBrush;

ImageBrush

Trang 40

HO CHI MINH UNIVERSITY OF INDUSTRY

Config Brush from VS design

Trang 41

2 Handling Application Events

From Code

Trang 42

HO CHI MINH UNIVERSITY OF INDUSTRY

2 Handling Application Events

From XAML

Trang 43

2.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 44

HO 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 46

2.Top-Level Elements

 Three classes are used by WPF programs as top-level

elements

– Application – Page

– Window

• Only one Content

Trang 47

3 Syntax for Object

One element

Empty element tag

Trang 48

HO CHI MINH UNIVERSITY OF INDUSTRY

3 Syntax for Object

Nested- Multi elements

Logical tree with four WFP

objects

XAML Tree With Four Elements

Trang 49

3 Syntax for Attribute

Object element syntax

– Like object syntax

• <button> FIT HUI </button>

Attribute syntax

Property Element Syntax

Attached Property Syntax

Trang 50

HO CHI MINH UNIVERSITY OF INDUSTRY

3 Syntax for Attribute

Trang 51

3 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 52

HO 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 53

3 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 54

HO 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 56

HO 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 57

5 XAML Namespace

Trang 58

HO CHI MINH UNIVERSITY OF INDUSTRY

5 XAML Namespace: Classes from Other Namespaces

Trang 59

Background = new LinearGradientBrush ( Colors Yellow, Colors Blue,

new Point (0,0), new Point (1,1));

}

}

} Create MyButton extend from class Button

Trang 60

HO CHI MINH UNIVERSITY OF INDUSTRY

xmlns : local ="clr-namespace:StudyWPFApplication"

Declare prefix:

In XAML

< local : MyButton Content ="Hello" ></ local : MyButton >

Instantiate the button:

Trang 61

5 Markup Extension

Trang 62

HO 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 64

HO 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 66

HO 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 67

Margin

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 68

HO CHI MINH UNIVERSITY OF INDUSTRY

6 XAML Layout: Align & Padding

Trang 69

6 Layout Panels

Canvas

for specific (X,Y) positioning

Trang 70

HO CHI MINH UNIVERSITY OF INDUSTRY

6 Layout Panels

StackPanel

Arranges content either horizontally or vertically

Trang 71

6 Layout Panels

WrapPanel

Arranges item sequentially

Trang 72

HO CHI MINH UNIVERSITY OF INDUSTRY

6 Layout Panels

DockPanel

Anchor elements to the edges of the container

Trang 73

6 Layout Panels

 Grid

Table-style layout of rows and columns

Trang 74

HO CHI MINH UNIVERSITY OF INDUSTRY

6 Layout Panels

 UniformGrid

All rows and columns are the same size

Contains two properties, Rows and Columns

Trang 76

HO CHI MINH UNIVERSITY OF INDUSTRY

Types of Containers

Trang 77

Types of Containers : Example

Trang 78

HO CHI MINH UNIVERSITY OF INDUSTRY

Trang 79

7.1 Border control

Container control can contains controls

Without border  using Border control

Accept single child control

Display border around child control

Trang 80

HO 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 82

HO 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 83

7.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 84

HO 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 85

7.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 86

HO 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 87

7.1 Basic Controls: Textbox control

Trang 88

HO CHI MINH UNIVERSITY OF INDUSTRY

7.1 Basic Controls: Textbox control

Trang 89

7.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 90

HO 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 91

7.1 Date control: Calendar control

Trang 92

HO 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 93

7.1 Date control: DatePicker control

Trang 94

HO 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 95

7.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 96

HO 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 97

7.1 Miscellaneous control

Example code 2

private void btn_Click

( object sender, RoutedEventArgs e) {

Trang 98

HO 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 100

HO 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

Ngày đăng: 15/03/2014, 15:20

TỪ KHÓA LIÊN QUAN

w