XML Developer Reference Free Sampler
XML Developer Reference Free Sampler
When you buy an ebook through oreilly.com you get lifetime access to the book, and
whenever possible we provide it to you in five, DRM-free file formats—PDF, .epub,
Kindle-compatible .mobi, Android .apk, and DAISY—that you can use on the devices of
your choice. Our ebook files are fully searchable, and you can cut-and-paste and print
them. We also alert you when we’ve updated the files with corrections and additions.
ISBN: 978-0-7356-5896-7
1 2 3 4 5 6 7 8 9 LSI 6 5 4 3 2 1
Microsoft Press books are available through booksellers and distributors worldwide. If you need support related
to this book, email Microsoft Press Book Support at [email protected]. Please tell us what you think of
this book at http://www.microsoft.com/learning/booksurvey.
The example companies, organizations, products, domain names, email addresses, logos, people, places, and
events depicted herein are fictitious. No association with any real company, organization, product, domain name,
email address, logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided without
any express, statutory, or implied warranties. Neither the authors, O’Reilly Media, Inc., Microsoft Corporation,
nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly
or indirectly by this book.
Silverlight. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
microsoft.com/learning/booksurvey
vii
Chapter 2 Object Elements and Attributes 19
XAML Is XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Root Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
XAML Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Default User Interface Element. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
XAML Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
The Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
The EventSetter and EventTrigger Classes. . . . . . . . . . . . . . . . . . . . . . . 81
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83
Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
viii Contents
Type Converters versus Markup Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . 98
XAML Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Security in XAML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100
Styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Defining Styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Implicit Styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Inheriting Styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
The Silverlight Toolkit Styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Styles vs. Control Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
More on Styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
The generic.xaml File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Triggers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126
ix
Part III XAML User Interface Controls
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210
x Contents
Creating and Binding to an ObservableCollection. . . . . . . . . . . . . . . . . . . . 234
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244
Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Ellipse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Rectangle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Rounded Rectangle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Polygon. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Polyline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Path. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Geometries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Brushes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Transforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285
Contents xi
Part V Appendixes
Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Index 303
microsoft.com/learning/booksurvey
xii Contents
C h apter 1
Introducing XAML
In this chapter:
■■ Windows Presentation Foundation (WPF)
■■ Silverlight
■■ Summary
You use the Windows Presentation Foundation (WPF) framework libraries along with the
ML-based eXtensible Application Markup Language (XAML) declarative markup language to define
X
and develop next-generation, abstracted, dynamic, rich, and interactive user interface layers that
provide data-integration capabilities and comprehensive support for multimedia, graphics, animation,
and documents.
3
Windows Presentation Foundation (WPF)
WPF supports the development of rich and interactive Windows desktop applications that can pro-
vide sophisticated and realistic user experiences. WPF is built upon a very different architecture than
Windows Forms. The key architectural differences are:
■■ WPF provides a new presentation framework that integrates XAML for user interface design.
The framework supports a unified programming model that includes data binding capabilities
to develop data-driven applications as well as media integration, 2-D and 3-D vector graphics,
document integration, text, and typography.
■■ WPF provides a set of .NET Framework libraries for the presentation core that are mainly
derived from the System.Windows namespace. These libraries handle integration of the
X AML-based user interface with the managed code-behind, including enhanced properties
and events integration, such as dependency properties and routed events (topics you’ll explore
later in this book).
■■ The new Media Integration Layer (MIL) provides a rendering engine for WPF applications built
upon Direct3D. The tight integration with DirectX means that WPF has high-performance
rendering of the visual interface that can take advantage of hardware acceleration using
the graphics processing units (GPUs) that most modern computers have, which reduces the
load on the central processing unit (CPU). This is a very different approach than that taken
in Window Forms applications, where the .NET Framework uses the User32 DLL to render
standard Window Forms user interface elements and uses older Graphics Device Interface
(GDI) to render graphics. Figure 1-1 illustrates the differences between the visual interface
rendering approaches for WPF and Windows Forms applications.
WPF
Presentation Core
Direct 3D
Operating System
User32
GDI
Device Drivers
Caution Not all XAML controls are interoperable between WPF, Silverlight, and WF
pplications. In addition, the XAML parsers for each platform are also different. You will
a
need to use and set appropriate WPF, Silverlight, and WF platform specific–XAML controls
and compile applications using the corresponding platform.
The XAML technical specification documentation provides details on XAML’s data model for types,
object hierarchies, and the techniques for mapping between XML and the object hierarchy data model.
It also documents the WPF and Silverlight vocabulary of types that can be used with XAML specifications.
Developers can use the WPF and Silverlight XAML technical specification documentation in conjunction
with publicly available standard specifications, computer language design, and implementation art to fully
understand and take advantage of XAML.
XAML Structure
Figure 1-2 provides a quick overview of defining a button. In the example, the button width is set to
100, the button background color is set to LightGray, and the content (the button label) is set to “I am
a Button“ in XAML. The example also shows an identical Button object created in C#, with its related
properties set in code.
I am a Button
XAML C#
Figure 1-2 Defining a Button object and its properties in XAML and in C# code-behind.
A XAML file has a .xaml file extension. As shown in Figure 1-2, any XAML file consists of XML-like
structured information that defines the relationships among various XAML controls. At runtime,
these controls render as an object tree to create the user interface. In other words, XAML itself is
an abstraction—it simply describes objects. This abstraction lets XAML serve as the UI description
More Info See Chapter 2, “Object Elements and Attributes,” for more details on XAML
syntax, XAML object elements, and attributes.
XAML’s capability to develop an externalized and loosely coupled user interface enables developers
to develop and modify the user interface without affecting the underlying program code and without
recompiling the project for each UI change, which can significantly reduce the overall effort required for
application development and testing.
C#
Compile (Design-time)
Button b1 = new Button( ); and Execute (Runtime)
b1.Width = 100; I am a Button
b1.Content = "I am a Button";
b1.Background = new
SolidColorBrush(Colors.LightGray);
Figure 1-3 Defining a Button object with its properties in XAML and using C# in code-behind.
Figure 1-4 demonstrates how you can define a style within a XAML file as a resource or as an
external resource file, and apply that style to a Button control.
Defining a Style
Button
<Style x:Key="ButtonStyle5" TargetType="Button">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="Green"/>
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="16"/>
</Style>
Applying a Style
More Info See Chapter 5, "Resources, Styles, and Triggers," for more details on styles and
resources for XAML.
Each control can exist in a number of possible states, such as disabled, having the input focus, a state
where the mouse is hovering over it, and so on. A control template lets you define what a control looks
like in each of these states. Sometimes this is referred to as changing the look and feel of the control,
because changing the visual appearance of each state alters how a user sees and experiences a control.
Figure 1-5 demonstrates how you can define a control template to change the appearance of a
Button control to make it look like an ellipse.
Applying a ControlTemplate
More Info See the article “Creating Interactive Bing Maps with Silverlight and IronRuby,” at
http://msdn.microsoft.com/en-us/magazine/ee291739.aspx for an example of how IronRuby
dynamic language integrates events of XAML objects to implement required business logic.
Each XAML file for WPF, Silverlight, or WF project has a corresponding code-behind file, which
Microsoft development tools such as Visual Studio or Expression Blend create for you automatically.
However, a third file type is associated with the XAML file. Figure 1-6 illustrates the full class imple-
mentation for the MainWindow XAML file of a standard WPF project created using either Visual
Studio or Expression Blend.
MainWindow Class
As part of build process
(or in Visual Studio
upon saving the XAML MainWindow.xaml.cs
file), a code-behind file
WPF XAML File is generated based on
XAML
MainWindow.xaml
MainWindow.g.i.cs
Code-behind files
create the WPF
MainWindow class
Note As defined on MSDN, “code-behind is a term used to describe the code that is joined
with markup-defined objects, when a XAML page is markup-compiled.” See http://msdn.
microsoft.com/en-us/library/aa970568.aspx to get more information on the code-behind
capabilities of XAML.
If you create a WPF application project by selecting WPF Application template in Visual Studio, you
will get a default MainWindow.xaml file. If you expand the MainWindow.xaml file in the Visual Studio
Solution Explorer, you will see an associated code-behind file named either MainWindow.xaml.cs file
(when you create a C# WPF project) or MainWindow.xaml.vb (when you create a Visual Basic WPF
project). This code-behind class is usually used to manage events and as a gateway to integrate with
other application components and services to implement the business logic.
The following code snippet demonstrates the default MainWindow.xaml.cs file of the WPF application
and the InitializeComponent() method (in bold font) within the class constructor:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
XAML defines the language features x:Class, x:Subclass, and x:ClassModifier directives, which (as you
will explore more deeply in Chapter 2) enable integration of the XAML markup file with the code-behind
partial class. You must derive the partial class defined in the root element of the XAML markup file using
the x:Class attribute. This class usually gets defined automatically by Visual Studio, using the naming
convention <XAMLFileName.xaml>.cs or <XAMLFileName.xaml>.vb, depending on which .NET language
you’re using. The following code snippet shows the definition of the x:Class attribute (in bold font) d
efined
in the Window root element of the MainWindow.xaml file of the WPF application.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
Inline Code
The WPF XAML namespace also supports an additional x:Code directive element that can contain
inline programming code (in C# or Visual Basic) to implement business logic directly within the XAML
file. Programming code within the x:Code element must be entered inside a <[CDATA[…]]> segment
so that it will be processed as code rather than as XML by the XAML parser.
The following example implements the Click event of a Button control within the XAML file as
inline code (in bold font). The code is written in C#, and is defined right next to the definition of the
Button control, but within an x:Code element:
Warning Despite the existence of the <x:code> element, inline coding within XAML is not
considered a best practice, and its use is not recommended except in special circumstances. It’s
defnitely not the best way to implement complex business logic. Inline code has some limitations
that make implementing reusable code across a project considerably more c hallenging. In
addition, it’s more difficult to code, maintain, and support complex business logic in inline code.
The inline code must be defined within the XAML file. The scope of inline code is limited to the scope
of the partial class created for that particular XAML instance.
The <x:Code> element must be an immediate child element of the root element of the XAML
production. Moreover, although XAML itself has the advantage of abstracting the user interface
definition of the application from the implementation of the business logic, inline code does not pro-
vide that abstraction, because it’s defined directly within the XAML file.
Caution The x:Code directive (and thus inline coding) is supported only by the WPF
XAML parser—it is not supported by the Silverlight XAML parser. Therefore, you cannot
implement inline coding in Silverlight applications. The Silverlight XAML parser also does
not guarantee preservation of CDATA segment content.
Silverlight
Silverlight is an extension of the .NET Framework–based technology platform to develop
cross-browser, cross-platform, and cross-device Rich Internet Applications (RIAs). RIAs are web
applications that have features and functionality similar to traditional desktop applications, including
rich and interactive user interfaces.
You can deploy Silverlight applications as plug-ins (in both in-browser and out-of-browser modes) that
run in a sandboxed environment. Silverlight is built upon lightweight components of the .NET Framework
that are a subset of the full WPF libraries. Silverlight applications do not require users to perform a
full install of the .NET Framework; instead, users need to install only a small Silverlight plug-in on their
Windows or Mac (Intel processer–based) computers, or Windows Phone 7 mobile devices.
Note To install the latest version of Silverlight and get the latest information on Silverlight,
visit Microsoft’s official Silverlight website at http://www.silverlight.net/getstarted/.
Like WPF Windows applications, the declarative XAML markup language used by Silverlight is at the
center of the declarative user interface (UI) framework. You can use the same Microsoft development tools
(Visual Studio and Expression Blend) to define Silverlight user interfaces in XAML and you can implement
business logic using standard .NET languages. However, there is a significant difference between the set of
XAML controls available for WPF and those available for Silverlight. In addition, the Silverlight platform has
limited .NET Framework libraries and its XAML parser as compared to the full WPF platform.
The initial versions of Silverlight (Silverlight 1.0 and 2.0 versions) were mainly targeted toward
building media applications, so it focused on media integration, vector graphics, and animation. Later
versions (Silverlight 3 and Silverlight 4) enhanced and streamlined Silverlight’s media applications
capabilities, and extended the product focus to implementing data-driven enterprise line of business
(LoB) applications. Silverlight 5 version extended the LoB applications capabilities and added support
for mobile applications development, as well as support for gaming and 3-D animations.
Note HTML5 has extended the HTML platform capabilities to support native markup
integration with multimedia (audio and video elements), enhanced user interface controls
(such as calendar, date, time, email, URL, and search elements), and content integration
(such as article, footer, header, and section elements). The new capabilities in HTML5 will
reduce the need for custom scripting and custom plugins (such as Adobe Flash) when
developing rich interactive applications. However, the implementation of the HTML5
specification is not standard. As a result, different browsers may take different approaches
and render HTML5 differently. This is a challenge for developers trying to develop HTML5-
based applications that provide consistent results across many different potential browser
targets. Offsetting this problem is the advantage that HTML is a cross-platform technology
platform supported by all modern operating systems (including operating systems such as
Windows, Linux, OS X, iOS, and Android).
Artists- Developers-
Define Visuals Implement Logic
Button
Button
XAML
XAML WCF
Data VB.NET,
Sources C#, ...
Data
Sources
Before ending this chapter, it’s worthwhile to provide a quick high-level overview of the key design-
time and runtime components of the .NET Framework so you’ll understand how they fit together.
Design-Time Components
Since .NET Framework 3.0, Microsoft has introduced the following key foundation components that
support a unified programming and deployment model:
■■ Windows Presentation Foundation (WPF), which uses XAML to define and develop abstracted,
dynamic, rich, and interactive user interface layer providing integration capabilities with data,
media, graphics, animation, and documents
■■ Windows Workflow Foundation (WF), which provides a framework for developing workflow-
driven task integration and automation
SOURCE CODE
Compile Time
.NET Base
Ruby
Class Library
Common Intermediate Language (CIL) / Libraries
(BCL)
Microsoft Intermediate Language (MSIL) Python
Custom Libraries
Assemblies BYTE CODE
Runtime
NATIVE CODE
Figure 1-8 Execution model of .NET Framework 4.0–based applications and services.
■■ The Common Language Runtime (CLR) is the underlying runtime execution engine of the
.NET Framework that provides a managed and secured application and services execution
environment. The CLR enables abstracted development of application components and
services using different languages, which get compiled to the common intermediate language
(CIL) bytecode format at design time. At runtime, CLR will translate CIL bytecode to the native
code using the Just-In-Time (JIT) compiler.
■■ Dynamic Language Runtime (DLR) is a .NET Framework hosting model for dynamic languages,
which provides a set of .NET Framework libraries and services for .NET and Silverlight that
create a bridge between dynamic languages and the CLR in .NET and Silverlight.
■■ Base Class Library (BCL) is a foundation for development of any .NET Framework–based
applications or services. It is a library of classes, interfaces, and value types that provides the
basic and system-level functionalities such as processing and managing XML files, integration
with database, integration with file systems and connected devices, animation, graphics and
media management, error and diagnostic management, integration with industry standard
protocols, providing encryption mechanisms, LINQ, parallel computing, and capabilities to
integrate with COM Interops.
Summary
The declarative XML-based eXtensible Application Markup Language (XAML) is at the center of the
WPF framework. You use XAML to define and develop abstracted, dynamic, rich, and interactive user
interface layers that can integrate with data, media, graphics, animation, and documents.
Using WPF, you can develop rich and interactive Windows desktop applications that can provide
sophisticated and realistic user experiences. Silverlight is a subset of WPF; you can use it to develop
browser-hosted cross-platform, rich Internet applications as well as applications for Windows 7.x
series mobile devices.
This chapter clearly shows that Microsoft has established a robust software and services development
and delivery platform—the Microsoft .NET Framework—built upon solid core software development
concepts such as object-oriented programming and language and environment independence. Together,
these support the following essential attributes of software platform and services:
■■ Dynamic and flexible services-focused architecture With the introduction of WPF, WCF,
and WF and support for managed code integration, you can now develop service-oriented
flexible software services using the .NET Framework deployable to both enterprise and cloud
platforms.
The next chapter, “Object Elements and Attributes,” will provide a detailed walkthrough of XAML
basics—all the key information you need to know to create visuals for the user interface layer of
XAML-driven applications.