If you want to learn how to build killer user interfaces for Windows and the web, then this book is for you. It arms you with the tools and code you'll need to effectively utilize the Windows Presentation Foundation (WPF). From creating appealing graphics and animated structures to enhancing performance and security, you'll be programming in no time.
First you'll explore the WPF framework and learn how to develop basic applications with ASP.NET or Visual Basic(r). Next you'll discover how to build more sophisticated WPF interfaces using Microsoft(r) ExpressionBlend and then progress to more advanced programming techniques.
Throughout the book, you'll find best practices for enterprise architectures using the WPF and its underlying technology. All this will help you quickly learn how to develop next-generation applications on the .NET 2.0 platform using the WPF.
What you will learn from this book
How to write applications with identical UIs on both Windows and the web
Tips for collaborating design and code development with Microsoft(r) Expression Blend
All about the object models, built-in server controls, HTML markup, code-behind, and the coordinative structure of each file
How to migrate Win32 applications to WPF
Techniques for integrating special effects and custom controls into an application
Advanced development concepts, including building workflows and a WCF service
Who this book is for
This book is for experienced .NET developers who want to begin creating WPF web and desktop applications.
Wrox Professional guides are planned and written by workingprogrammers to meet the real-world needs of programmers, developers, and IT professionals. Focused and relevant, they address the issues technology professionals face every day. They provide examples, practical solutions, and expert education in new technologies, all designed to help programmers do a better job.
Chris Andrade is a Principal with Novera Consulting, a Microsoft Certified Partner specializing in enterprise architecture and development with .NET and Microsoft server technology. Chris devotes most of his time to assisting companies in applying Microsoft technologies to improve their business processes and operations. Chris has worked within a diverse range of business verticals, including automotive, healthcare, and mortgage. Chris also takes active participation within the local developer community, speaking and presenting along the west coast whenever possible.
Shawn Livermore (MCAD, MCSD, PMP) [shawnlivermore.blogspot.com] has been architecting and developing Microsoft-based solutions for nearly a decade. Shawn has been consulting as an enterprise and solutions architect for Fortune 500 clientele within highly visible enterprise implementations. His range of technical competence stretches across platforms and lines of business, but he specializes in Microsoft .NET enterprise application architectures and Microsoft server-based product integrations. Shawn lives in the Southern California area with his beautiful wife Shantell and amazing daughter Elexzandreia. Shawn also enjoys beating his friend Jason at air hockey, basketball, baseball, football, arcade games, cards, billiards, ping pong, shuffleboard, trivia, golf, racquetball, dirt bike races, crosscountry skiing... and pretty much any other sport in existence. Shawn would like to once again remind Jason, “Who’s your daddy?”
Mike Meyers is president of Novera Consulting Inc, a software development and consulting firm specializing in custom application development utilizing the.NET platform and Microsoft server technology. Based in Orange County, California, Mike’s company is focused on providing solutions based on Microsoft platforms and technology to companies spanning multiple industries. Mike has worked in a number of industries, including mortgage, healthcare, and various ecommerce ventures as developer, architect, and project manager. When he’s not writing code, Mike is active mountain biking in southern California, hiking with his four-legged Labrador Dakota or playing music with friends.
Scott Van Vliet is an accomplished Solutions Architect who has spent the past decade delivering successful Microsoft-based solutions to his clients. Currently a Senior Manager with Capgemini, a worldwide leader in technology consulting services, Scott has managed, architected, and developed solutions for companies across the globe. He is also the Microsoft Delivery Leader for Capgemini’s Telecom, Media & Entertainment practice, providing technical and engagement leadership on all Microsoft-based projects. Scott can be reached via his Web site at http://www.scottvanvliet.com/.
If you want to learn how to build killer user interfaces for Windows and the web, then this book is for you. It arms you with the tools and code you'll need to effectively utilize the Windows Presentation Foundation (WPF). From creating appealing graphics and animated structures to enhancing performance and security, you'll be programming in no time.
First you'll explore the WPF framework and learn how to develop basic applications with ASP.NET or Visual Basic(r). Next you'll discover how to build more sophisticated WPF interfaces using Microsoft(r) ExpressionBlend and then progress to more advanced programming techniques.
Throughout the book, you'll find best practices for enterprise architectures using the WPF and its underlying technology. All this will help you quickly learn how to develop next-generation applications on the .NET 2.0 platform using the WPF.
What you will learn from this book
How to write applications with identical UIs on both Windows and the web
Tips for collaborating design and code development with Microsoft(r) Expression Blend
All about the object models, built-in server controls, HTML markup, code-behind, and the coordinative structure of each file
How to migrate Win32 applications to WPF
Techniques for integrating special effects and custom controls into an application
Advanced development concepts, including building workflows and a WCF service
Who this book is for
This book is for experienced .NET developers who want to begin creating WPF web and desktop applications.
Wrox Professional guides are planned and written by workingprogrammers to meet the real-world needs of programmers, developers, and IT professionals. Focused and relevant, they address the issues technology professionals face every day. They provide examples, practical solutions, and expert education in new technologies, all designed to help programmers do a better job.
For those of us who have been developing applications to run on the Windows platform, the topic of this book presents a compelling and exciting wave of change to how such applications can be built. In addition, for those of us who have been developing web-based applications, an even more exciting shift is approaching.
Windows Presentation Foundation, also known as WPF, is the next-generation graphics platform on which both Windows- and web-based applications can be built to run on Windows Vista, the latest evolutionary release of the Windows operating system. WPF provides the foundation for introducing an elegant and high fidelity User Experience (UX) by juxtaposition of user interface, application logic, documents, and media content.
Although originally targeted solely for Windows Vista, WPF will be made available for Windows XP and Windows Server 2003 as part of the .NET Framework 3.0 (formerly WinFX) developerplatform.
This coalescence of form and function is further empowered by tools such as XAML and the Microsoft Expression Designers, which allow designers and developers to work in parallel on the user interface and the application logic, coming together to provide a seamless UX.
This chapter provides an overview of WPF, including the following key topics:
Evolution of the Windows API
.NET Framework 3.0, the next-generation APIs for Windows-based development
WPF architecture and development model
XAML, the new declarative language backing WPF
Tools to develop WPF applications
A Brief History of the Windows API
The Windows API exposes the core set of functionality provided by the Windows operating system for use in applications. Primarily designed for C/C++ development, the Windows API has been the most direct mechanism with which an application can interact with Windows.
The Windows API comprises the following functional groups:
Base Services - Provides access to the core resources of the computer, such as memory, filesystems, devices, processes, and threads
Common Control Library - A collection of common control windows used throughout the Windows operating system, providing the distinctive Windows look and feel
Common Dialog Box Library - A collection of dialog boxes used to execute common tasks, including file opening, saving, and printing
Graphics Device Interface (GDI) - Provides the facilities for an application to generate graphical output to displays, printers, and other devices
Network Services - Provides access to various networking capabilities of the Windows operating system, including RPC and NetBIOS
User Interface (UI) - Provides the mechanism for managing windows and controls in an application and input from devices such as the mouse and keyboard
Windows Shell - The container that organizes and presents the entire Windows UI, including the desktop, taskbar, and Windows Explorer
Through these services, developers have had significant flexibility in creating powerful applications for the Windows operating system. However, this flexibility also bore the responsibility of handling low-level and often tedious operations.
With each new release of the Windows operating system, additions and updates to the Windows API were almost always included. Yet with each release, Microsoft strived to support backwards compatibility. Thus, many functions included in the original Windows API still exist today in Windows XP and Windows Server 2003.
The following list includes major versions of the Windows API:
Win16 - The API for the first 16-bit versions of Windows
Win32 - Introduced in Windows NT, the API for 32-bit versions of Windows
Win32 for 64-bit Windows - Formerly known as Win64, the API for 64-bit versions of Windows XP and Windows Server 2003
Platform Evolution
Since the release of Windows 1.0 more than 20 years ago, the GDI and the UI services of the Windows API have provided a reliable graphics platform for Windows applications. Many applications we use on a day-to-day basis, including the Microsoft Office suite and Internet Explorer, are built on this foundation.
Although this graphics platform has seen many successes, fundamental changes have occurred in technology since its initial appearance more than two decades ago. With the proliferation of the personal computer and the increasing availability of broadband Internet access, the demand for rich visual experiences has dramatically increased. Moreover, advancements in graphics hardware technology have paved the way for astounding digital media and interactive entertainment products. Facing this new demand for a rich visual experience, Microsoft has invested heavily in providing such an experience as part of the next-generation Windows API - .NET Framework 3.0.
Introducing .NET Framework 3.0
.NET Framework 3.0 is a revolutionary milestone for developing applications on the Windows operating system. Built atop the .NET Framework 2.0, .NET Framework 3.0 is a set of managed APIs that provide enhanced functionality for messaging, workflow, and presentation.
Key components of .NET Framework 3.0 include:
Windows Presentation Foundation (WPF) - The graphical subsystem for all things related to the UI
Windows Communication Foundation (WCF) - The messaging subsystem of .NET Framework 3.0, securing program communication through a single API
Windows Workflow Foundation (WF) - Provides workflow services for applications built to run on Windows
As the new programming model for Windows Vista, .NET Framework 3.0 fuses the Windows platform with applications developed using Visual Studio 2005. With direct access to low-level services of the operating system and hardware surface, .NET Framework 3.0 provides a compelling solution for creating applications with a rich UX.
Although WCF and WF are equally important components of .NET Framework 3.0, they are beyond the scope of this book. For more information on WCF and WF, visit the .NET Framework Developer Center on MSDN (http://msdn2.microsoft.com/en-us/netframework).
Meet Windows Presentation Foundation
Formerly known as Avalon, Windows Presentation Foundation (WPF) is the new graphical subsystem in Windows Vista that provides a holistic means for combining user interface, 2D and 3D graphics, documents, and digital media. Built on the .NET Framework, WPF provides a managed environment for development with the Windows operating system. This takes advantage of the existing investment made by Microsoft in the .NET Framework, and allows developers familiar with .NET technologies to rapidly begin developing applications that leverage WPF.
Guiding Design Principles
To enhance UX and empower both designers and developers, WPF has been built under the following design principles:
Integration
Vector graphics
Declarative programming
Simplified deployment
Document portability
Integration
In today's world, developing a Windows application may require the use of any number of different technologies, ranging from GDI/GDI+ for 2D graphics, UI services (User32 or WinForms), or Direct3D or OpenGL for 3D graphics. On the contrary, WPF was designed as a single model for application development, providing seamless integration between such services within an application. Similar constructs can be used for developing storyboard animation, data bound forms, and 3D models.
Vector Graphics
To take advantage of new powerful graphics hardware, WPF implements a vector-based composition engine. This allows for graphics to scale based on screen-specific resolution without loss of quality, something nearly impossible with fixed-size raster graphics. WPF leverages Direct3D for vector-based rendering, and will utilize the graphics processing unit (GPU) on any video card implementing DirectX 7 or later in hardware. In anticipation of future technology, such as high-resolution displays and unknown form factors, WPF implements a floating-point logical pixel system and supports 32-bit ARGB colors.
Declarative Programming
WPF introduces a new XML-based language to represent UI and user interaction, known as XAML (eXtensible Application Markup Language - pronounced "zammel"). Similar to Macromedia's MXML specification, within XAML elements from the UI are represented as XML tags. Thus, XAML allows applications to dynamically parse and manipulate UI elements at either compile-time or runtime, providing a flexible model for UI composition.
Following the success of ASP.NET, XAML follows the code-behind model, allowing designers and developers to work in parallel and seamlessly combine their work to create a compelling UX. With the aid of design-time tools such as the Visual Designer for Windows Presentation Foundation add-in for Visual Studio 2005, the experience of developing XAML-based applications resembles that of WinForms development. Moreover, designers accustomed to visual tools such as Macromedia Flash 8 Professional can quickly ramp-up to building XAML-based solutions using visual design tools such as Microsoft Expression Blend. These tools are covered later in this chapter and throughout this book.
Simplified Deployment
WPF applications can be deployed as standalone applications or as web-based applications hosted in Internet Explorer. As with smart client applications, web-based WPF applications operate in a partial-trust sandbox, which protects the client computer against applications with malicious purpose.
Furthermore, WPF applications hosted in Internet Explorer can exploit the capabilities of local client hardware, providing a rich web experience with 3D, digital media, and more, which is the best argument for web-based applications available today.
Document Portability
Included in WPF is an exciting new set of document and printing technologies. In conjunction with the release of Microsoft Office 12, WPF utilizes Open Packaging Conventions, which supports compression, custom metadata, digital signatures, and rights management. Similar to the Portable Document Format (PDF), the XML Paper Specification (XPS), which allows for documents to be shared across computers without requiring that the originating application be installed, is included in WPF.
Architecture
The anatomy of WPF consists of unmanaged services, managed subsystems, and a managed API available for consumption by WPF applications, known as the presentation framework.
Figure 1-1 outlines the general underlying architecture of WPF, with each major component detailed in the sections that follow.
Element System
The element system represents the surface of WPF with which developers will interact. Contained within the element system are the core components making up the UI, such as styles, layout, controls, binding, and text layout.
Almost all the components in the element system derive from the all-important System.Windows .FrameworkElement class. This class provides the base functionality required to interact with WPF core presentation service, and implements key members of its parent class, System.Windows.UIElement, which provides functionality for all visual elements within WPF for layout, eventing, and input. The UIElement class can be compared to HWND in Win32, and is the starting point for input-specific functionality within the WPF inheritance hierarchy. The FrameworkElement and related classes are discussed further in Chapter 2.
Most of the topics covered within this book related to WPF application development pertain to components and features of the element system. However, the underlying subsystems are indeed used extensively by WPF applications, and will be noted where appropriate.
Element Trees
An important concept to grasp in WPF is the notion of element trees, which represent the visual elements of which a WPF application is comprised. Two element trees exist within a WPF application: the logical tree and the visual tree.
Logical Tree
The logical tree is the hierarchical structure containing the exact elements of your WPF application as defined either declaratively within a XAML file, or imperatively in code. Consider the following XAML code snippet:
When compiled for or parsed under WPF, this code snippet would yield a logical tree like that depicted in Figure 1-2.
The logical tree outlines the 1:1 mapping of the nested XAML elements declared in the code snippet to their appropriate classes in the WPF API. The top-level element is ElementTrees.Sample, which derives from System.Windows.Window, the top-most, non-navigable container for a WPF application.
The Window class itself derives from System.Windows.Controls.ContentControl, which is the base class for all controls containing a single piece of content within WPF. To provide support for context-aware layout, the System.Windows.Controls.Panel class is available and includes functionality to arrange and lay out elements in WPF. The next element in the logical tree for the preceding code snippet is System.Windows.Controls.StackPanel, which derives from Panel. StackPanel arranges all client elements of the control in a single line, oriented either horizontally or vertically. This control behaves similarly to an HTML table with a single column or row, depending on orientation.
Contained within the StackPanel instance are the controls visible to the user: System.Windows.Controls.TextBox, System.Windows.Controls.Button, and System.Windows.Controls.Label. Although ubiquitous, these controls are unique implementations created from the ground up for WPF. Although they may behave in much the same way as their counterparts in WinForms from a user's perspective, their composition and behavior within the programming model and the visual tree are quite different.
Visual Tree
As its name implies, the logical tree makes sense - each element in the application is represented by a corresponding instance created by WPF. Under the hood, however, much more is taking place.
For each element in the logical tree, additional elements may be created to represent visual aspects required by the element. The combination of application elements and the elements created for visualization support is known as the visual tree. Considering the previous code snippet, the visual tree shown in Figure 1-3 would result.
Okay, take a moment to catch your breath. WPF does a lot of work under the hood to support the flexible visual model implemented therein. In addition to the elements of the application included in the logical tree, 20 additional elements are created to support visualization. The following are some of these elements created for the ElementTrees.Sample element:
System.Windows.Controls.Border - This control will draw a border and/or background surrounding its nested elements. The Border control is commonly found in many default controls, such as System.Windows.Controls.Canvas, System.Windows.Controls.Label, and System.Windows.Controls.TextBox.
System.Windows.Controls.Grid - Similar to an HTML table, the Grid control provides a flexible area for content layout and positioning using rows and columns. Note, however, that there is a distinct Table control, System.Windows.Documents.Table, which provides block-level content flow also based on rows and columns and derives from a different class than Grid, which derives from System.Windows.Controls.Panel.