Tải bản đầy đủ (.pdf) (137 trang)

Chapter 5: Windows Presentation Foundation - Do Thi Ngoc Quynh

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.3 MB, 137 trang )

Chapter 5.
Windows Presentation Foundation
Do Thi Ngoc Quynh
Ha Noi University of Technology

1


Objectives
“The Windows Presentation Foundation is a vital
component to the future of application development,
allowing developers to take control of the sheer power
that is available at the desktop to develop rich,
interactive, media-enhanced user interfaces...”

Microsoft

2


Agenda











WPF Introduction
XAML
Programming WPF Applications
Building Your First WPF Application
Exploring the Layout Controls
Working with XAML Controls
Working with Graphics, Media and Animations
New in WPF 3.5
Future Directions of WPFSummary

Microsoft

3


Part 1


WPF Introduction…

Microsoft

4


Presentation Layer Architecture: From
Yesterday to Tomorrow

Punch-card request/response
architecture

Microsoft

Terminal-based request/response
architecture
5


Presentation Layer Architecture: From
Yesterday to Tomorrow

Request/response architecture of user graphical user interface (GUI) application
talking to mainframe
Microsoft

6


Presentation Layer Architecture: From
Yesterday to Tomorrow

Web browser–based request/response architecture
Microsoft

Sophisticated browser architecture
7


Presentation Layer Architecture: From
Yesterday to Tomorrow


The .NET Framework provides consistent browser, desktop, and server application
programming interfaces (APIs)

Microsoft

8


How you develop your client
applications?


One set of technologies—based on HTML, DHTML,
Asynchronous JavaScript and XML (Ajax), and Microsoft
ASP.NET Atlas




gives you the same level of sophistication that a “heavy” client
can give you using “light” technologies

The other approach: WPF


Microsoft

including a declarative XML markup language named Extensible
Application Markup Language (XAML) that defines the user
interface, but has an incredibly powerful runtime supporting it


9


The Windows SDK


The Windows SDK contains all the APIs for developing
next-generation Windows applications




provides the foundation for software development for the
Windows server and client platforms for the foreseeable future

The Windows SDK is centered on the .NET Framework
3.0, sometimes called NetFX


Microsoft

which contains the classes you are familiar with from the .NET
Framework 2.0 in addition to WPF, WCF, and WWF

10


Windows Presentation Foundation
(WPF)




Latest UI development platform from MSFT
Integration of:






2D graphics
3D graphics
video/audio/animation

Declarative/Procedural programming model:



Microsoft

XAML
C#/Visual Basic/etc…

11


WPF Features and Machinery



Control library






Layout panels





canvas, stack, wrap, doc panels
grid – most flexible

Actions






buttons, sliders, menus, toolbars
tool tips, popups, scroll bars, etc…
user defined as well

events
commands
Triggers


Styles, skins, themes, templates

Microsoft

12


WPF - Architecture

Microsoft

13


Tools of the Trade


If you want to get started developing WPF applications,
you’ll need to download and install the various tools,
runtimes, and SDKs



Visual Studio 2005 or Visual Studio 2008/2010
Microsoft Expression







Microsoft

/>
The WinFX Runtime Components
The Windows Software Development Kit (SDK)
Visual Studio Development Tools for WinFX

14


Part 2


XAML...

Microsoft

15


What is XAML?


Extensible Application Markup Language





For describing hierarchies of objects

For:




Microsoft

Developers
Designers
Code generators


Why do we need XAML?




Used by WPF, WF
Also usable for your own purposes
No more choosing between C# or VB.NET

Microsoft


XAML vs. Code


XAML and code can do the same thing

Xml tag
creates object

Content=“Hello WPF!”
Width=“100”
Height=“50”
>

Microsoft

Button b = new Button();
b.Content=“Hello WPF!”;
b.Width=100;
b.Height=50;
Xml attribute
sets property
value


Attributes and Events


Install event handler
When button is
clicked

Attributes can also set event handlers


Content=“Hello WPF!”
Width=“100”
Height=“50”
Click=“buttonClick”
>

Microsoft

Button b = new Button();
b.Content=“Hello WPF!”;
b.Width=100;
b.Height=50;
b.Click+= buttonClick;


Nesting elements


Nesting elements means nesting objects

<Canvas>
Content=“Hello WPF!”
Width=“100”
Height=“50”
Click=“buttonClick”
>
</Canvas>

Microsoft


Canvas c= new Canvas();
Button b = new Button();
b.Content=“Hello WPF!”;
b.Width=100;
b.Height=50;
c.Children.Add(b);
Nesting adds one
object
to another object
(if supported)


XAML Namespaces


How does XAML know which Button?
xmlns=“ />Content=“Hello WPF!”
Width=“100”
“using”
Height=“50”
Click=“buttonClick”
“Imports”
>



This namespace gets mapped to




XAML allows mapping Xml namespaces to be mapped
to .NET namespaces

Microsoft


Using other namespaces


WPF uses its own namespace by default




Less typing

XAML has its own namespace with x: prefix
– Used for XAML specific keywords

- x:Type, x:Null, x:Class, x:Name, x:Key, …

Microsoft


Adding your own mapping







Use your own classes
– Class needs default constructor
Map a xmlns prefix to a CLR namespace

Use prefix with tag

Microsoft

Whatever string you
like!


Mapping Multiple Namespaces


WPF maps several
namespaces as one



















Microsoft

System.Windows
System.Windows.Automation
System.Windows.Controls
System.Windows.Controls.Primitives
System.Windows.Data
System.Windows.Documents
System.Windows.Forms.Integration
System.Windows.Ink
System.Windows.Input
System.Windows.Media
System.Windows.Media.Animation
System.Windows.Media.Effects
System.Windows.Media.Imaging
System.Windows.Media.Media3D
System.Windows.Media.TextFormatting
System.Windows.Navigation
System.Windows.Shapes

xmlns=“ />06/xaml/presentation”



WPF Properties


How does XAML convert strings to …?
String

Content=“Hello WPF!”
Width=“100”
Height=“50”
>
String

Microsoft

String
Button b = new Button();
b.Content=“Hello WPF!”;
b.Width=100;
b.Height=50;
Double


×