Developing Windows Presentation Foundation Applications Introduction Microsoft® Windows® Presentation Foundation provides a new model and tools for developers to build client applicati
Trang 1real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred Complying with all applicable copyright laws is the responsibility of the user Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation
The names of manufacturers, products, or URLs are provided for informational purposes only and Microsoft makes no representations and warranties, either expressed, implied, or statutory, regarding these
manufacturers or the use of the products with any Microsoft technologies The inclusion of a manufacturer or product does not imply endorsement of Microsoft of the manufacturer or product Links are provided to third party sites Such sites are not under the control of Microsoft and Microsoft is not responsible for the contents of any linked site or any link contained in a linked site, or any changes or updates to such sites Microsoft is not responsible for webcasting or any other form of transmission received from any linked site Microsoft is providing these links to you only as a convenience, and the inclusion of any link does not imply endorsement of Microsoft of the site or the products contained therein
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks,
copyrights, or other intellectual property
© 2006 Microsoft Corporation All rights reserved.
Microsoft, Microsoft Press, Aero, BizTalk, ClearType, Expression, IntelliSense, Internet Explorer, MSDN, MSN, Segoe, Tahoma, Visual Basic, Visual C#, Visual Studio, Visual Web Developer, Windows, Windows Media, Windows Server, Windows Vista, and WinFX are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries
The names of actual companies and products mentioned herein may be the trademarks of their respective owners
Trang 2Developing Windows Presentation Foundation
Applications
Introduction
Microsoft® Windows® Presentation Foundation provides a new model and tools for
developers to build client applications that combine advanced UI elements and provide
easy access to content from a variety of sources This session provides an overview of
how to create applications by using Windows Presentation Foundation
Objectives
After completing this section, you will be able to:
• Describe Windows Presentation Foundation
• Explain XAML
• Create applications by using XAML
• Create a Windows Presentation Foundation application
• Manage windows in XAML
• Navigate application pages in XAML
• Build XAML browser applications
• Create rich 2-D and 3-D content
• Use data binding in Windows Presentation Foundation
Trang 3Introducing Windows Presentation Foundation
The Microsoft Windows Presentation Foundation provides the foundation for building
applications in Microsoft Windows Vista™, blending together application UI, documents,
and media content, while exploiting the full power of your computer Windows
Presentation Foundation is exposed through the Microsoft NET Framework Windows
Presentation Foundation consists of:
• A display engine that takes full advantage of modern graphics hardware
• An extensible set of managed classes that developers can use to create rich, visually
stunning applications
• Extensible Application Markup Language (XAML), which enables developers to use
an XML-based model to declaratively manipulate the object model
Windows Presentation Foundation is based on the following design principles:
• Integration of various content sources into a single model Windows Presentation
Foundation provides a single model that can be used to manipulate content from a
variety of media sources, document sources, and user interface sources and allows
seamless integration of content within a single application You can use the same
constructs for animation, data binding, and styling regardless of whether you are
targeting 2-D, 3-D, or text content
• Use of vector-based graphics The composition engine is vector-based, allowing for
scaling of all output to match the resolution of a specific machine
Trang 4• Support for declarative programming The use of XAML allows applications to parse and manipulate UI logic at run time for dynamic workflow scenarios Furthermore, the XAML/code-behind model embodied in Windows Presentation Foundation allows designers and developers to work collaboratively on client application design and development
• Easy deployment Web-browser applications run from within Microsoft Internet Explorer®, either occupying the entire window or fitting in an inline frame Stand-alone applications are locally installed via ClickOnce or MSI technologies
• Document life-cycle options Windows Presentation Foundation introduces a new set
of document and print technologies
Powerful 3-D graphics hardware that is now standard in most computers is today utilized for most applications The Windows Presentation Foundation sets out to take full advantage of these capabilities as a platform for building rich, immersive, user
under-experiences
Trang 5What Is XAML?
XAML (pronounced “zamel”) is a new declarative language for creating application user
interfaces It offers a wealth of new controls and elements with capabilities such as forms
development, animation, and rendering of 2-D and 3-D graphics
When a developer creates an application in Windows Presentation Foundation, most of
the code is written and implemented in XAML XAML code must be well-formed XML,
be saved in a file with the xaml extension, and have a namespace that maps to XML
namespaces
XAML provides the following benefits:
• Faster and easier application implementation Markup-based interfaces are quick to
build and are easily modifiable They require less code than traditional structured
programming
• Easier to localize Because XAML separates the user interface from the application
logic, it is easier to localize the user interface This also gives the ability for
developers to modify application logic without affecting the user interface
Trang 6• Familiar development environment Developers with experience using HTML or other Web-oriented markup languages will find XAML a familiar environment for developing applications
• Third-party support XAML can be developed in many different development tools, which offers third-party developers opportunities to create applications by using a variety of visual tools
• Extensible Using object-oriented programming techniques, developers can extend XAML by creating custom controls, elements, and functionality
• No performance penalty With XAML, there is no performance penalty because XAML is an XML-based representation of the object model
Trang 7Programming with XAML
XAML is the preferred way to create a UI in the Windows Presentation Foundation
programming model because it enables you to separate UI definition from logic It also
enables you to integrate code by using code-behind files that are joined to the markup
through partial class definitions For simple cases, you can also specify inline code
Every XAML tag corresponds directly to a NET Framework class whose properties are
controlled through the use of XML attributes For example, the <Button> tag corresponds
directly to the System.Windows.Controls.Button class
Each XAML file includes the following elements:
• Root element As with all well-formed XML files, XAML files must have one root
tag When creating the XAML file, use a root element such as a DockPanel or
Canvas that can support a content model so that you can add child elements or other
content to it or an element such as Window or Page that is part of the application
model
The root element contains Web links references to two of the schemas used by
XAML files These schemas are available at: http://schemas.microsoft.com/winfx/
2006/xaml/presentation and http://schemas.microsoft.com/winfx/2006/xaml These
schemas contain the definitions of graphical elements represented by WPF and often
have a corresponding object associated with them
Trang 8• Properties XAML properties that are expressed as attributes enable you to add
features to object element tags Often, the properties that appear as XAML attributes
on an element are inherited from base classes For example, the Background
property is not an immediately declared property on the Button class but is instead inherited from the base Control class
• Name property Many XAML elements support a Name property This property is
particularly important because it allows you to reference a particular element from code-behind when that element was originally created from XAML markup
The following code illustrates a simple XAML file:
Trang 9Demonstration 1: Writing and Building a XAML Application
In this demonstration, you will see how you can write and build a XAML application
Key Points
The key points of this demonstration are:
• Every XAML file includes some core elements
• You can use a variety of tools to create a XAML application, including XamlPad and
Microsoft Visual Studio® 2005
Trang 10Creating a Windows Presentation Foundation Application
You can create a WPF application by using a code-behind file or by using inline code
Most applications consist of both XAML and code, such as Microsoft Visual Basic® or
Microsoft Visual C#®, in a code-behind file As a best practice, you should avoid or limit
the use of inline code
Using the code-behind model
When you use a code-behind model, the code for the application is contained within a
class file separate from the XAML file that displays the user interface This model is
similar to the code-behind model used with Microsoft ASP.NET The model allows a
clean separation of the XAML content from the application code The root XAML
element in an XAML file identifies where the application expects to find the code-behind
file when it compiles The root element specifies a namespace and class as its x:Class
attribute value
Trang 11Using inline code
You can also embed your code within a XAML file You do this by using the x:Code
directive element, which can contain inline programming code to interact with the XAML on the same page
Trang 12Demonstration 2: Developing XAML Applications in
Visual Studio 2005
In this demonstration, you will see how you can develop XAML applications in
Visual Studio 2005
Key Points
The key points of this demonstration are:
• Visual Studio 2005 provides the Design, Xaml, and Source views of a XAML
application
• You can use Visual Studio 2005 to add controls, text, and event handling to an
XAML application
Trang 13Managing Windows
Windows Presentation Foundation applications typically have at least one and sometimes
several windows Windows are used for a variety of purposes, including hosting
graphical UIs, displaying graphics or text information, and handling user interaction
The Windows Presentation Foundation Application class provides basic support for
windows In particular, the Application class handles message dispatching so that you do
not need to implement a message pump or window procedure The Application object
maintains a collection of references to the windows in your application By default,
closing the last window shuts down the application
In Windows Presentation Foundation, you can represent windows by using one of these
three objects:
• Window object Supports core window functionality
• NavigationWindow object Extends the Window object to support navigation from
one XAML page to another
• Page object The Page object, which enables you to set several window properties in
markup, is typically used as the root element of the pages that make up a navigation
application Use the Page object to define pages that you need to navigate to
Trang 14Windows Presentation Foundation applications have a window that is designated as the main window When you start your application, the first window that you create becomes
the main window You can get a reference to that window from the MainWindow property of the Application object Because this is a read/write property, you can change your main window by assigning another window to MainWindow
Application hosting and window management
You can compile Windows Presentation Foundation applications to run as a XAML browser application or a Windows application These two hosting scenarios impose different requirements for window use as shown in the following table
Requirement XAML Browser Application Windows Application
window Window types Implicitly uses a NavigationWindow Can use either Window or
NavigationWindow
Window properties Can specify only three properties:
Title, Height, and Width
Can specify all properties
New windows Can launch only pop-up elements,
and they must stay within the window border
Can launch additional windows and dialog boxes
Trang 15Navigating Application Pages
The Windows Presentation Foundation framework provides you with a powerful and
flexible way to control the flow of your program It is particularly useful if your
navigation targets must be determined at run time from user feedback or external data or
you need to navigate a hierarchy of information Although structured navigation can be
used for stand-alone applications, it is typically used for relatively short applications that
are called by larger applications This type of program guides users through complex
procedures in which the next step in the procedure is often not determined until the user
has provided input
When users work with a multi-page application, they typically proceed through a number
of pages Each time they do so, they unload the current page and load the next page,
usually called the target page With Windows Presentation Foundation, you can use one
of several techniques to navigate from one page to the next
• Navigate to a specified URI
• Navigate to a specified page object
• Navigate forward or backward in the journal
Trang 16For example, you can navigate to a new XAML page by using the following code:
private void btnGoToPage2(object sender, RoutedEventArgs args)
the target page function to the previous page function, you need to call the OnReturn
method When you call this method, the system returns you the previous page function
in the stack You do not need to know anything about the previous page function This feature allows you to call a page function from any other page in your application and easily return to that page You can also pass data in this fashion across pages