Flylib.com

Books Software

 
 
 

Introducing WinFX(TM) The Application Programming Interface for the Next Generation of Microsoft Windows Code Name Longhorn (Pro Developer) - page 24


Summary

You ve now mastered the basics. You can write XAML and can compile, deploy, and run the resulting application. Unfortunately, the applications you ve learned to write so far are pretty boring. Chapter 3 dives into XAML in depth and shows you how to use a wide variety of UI objects provided by the Longhorn platform. Later chapters show you a number of the other new technologies that you can also use in your applications.



Chapter 3: Controls and XAML

Overview

As you ve seen in Chapter 2, Longhorn platform applications typically consist of an Application object and a set of user interface pages that you write in a declarative markup language called XAML.

The Application object is a singleton and persists throughout the lifetime of the application. It allows your application logic to handle top-level events and share code and state among pages. The Application object also determines whether the application is a single window application or a navigation application.

You typically write each user interface page using a dialect of XML named Extensible Application Markup Language (XAML). Each page consists of XAML elements, text nodes, and other components organized in a hierarchical tree. The hierarchical relationship of these components determines how the page renders and behaves.

You can also consider a XAML page to be a description of an object model. When the runtime creates the page, it instantiates each of the elements and nodes described in the XAML document and creates an equivalent object model in memory. You can manipulate this object model programmatically ” for example, you can add and remove elements and nodes to cause the page to render and behave differently.

Fundamentally, a XAML page describes the classes that the runtime should create, the property values and event handlers for the instances of the classes, and an object model hierarchy ”that is, which instance is the parent of another instance.

All XAML documents are well-formed XML documents that use a defined set of element names. Therefore, all rules regarding the formation of well- formed XML documents apply equally to XAML documents. For example, the document must contain a single root element; all element names are case- sensitive; an element definition cannot overlap another element definition but must entirely contain it, and so on. If you re not familiar with XML syntax, now is an excellent time to learn it.



XAML Elements

Each XAML page contains one or more elements that control the layout and behavior of the page. You arrange these elements hierarchically in a tree. Every element has only one parent. Elements can generally have any number of child elements. However, some element types ”for example, Scrollbar ” have no children; and other element types ”for example, Border ” can have a single child element.

Each element name corresponds to the name of a managed class. Adding an element to a XAML document causes the runtime to create an instance of the corresponding class. For example, the following markup represents a root DockPanel element that has a single child Table element. The Table element contains three child Row elements. Each Row element contains three children, and a few of them have child text nodes.

<Border xmlns="http://schemas.microsoft.com/2003/xaml"

Background="BlanchedAlmond">
<DockPanel>
<Table>
<Body>
<Row>
<Cell><Button/></Cell>
<Cell><Text>Item</Text></Cell>
<Cell><Text>Price</Text></Cell>
</Row>
<Row>
<Cell><CheckBox Checked="true"/></Cell>
<Cell><TextBox Height="50">Nissan 350Z</TextBox></Cell>
<Cell><TextBox Height="50">29.95</TextBox></Cell>
</Row>
<Row>
<Cell><CheckBox/></Cell>
<Cell><TextBox Height="50">Porsche Boxster</TextBox></Cell>
<Cell><TextBox Height="50">9.95</TextBox></Cell>
</Row>
</Body>
</Table>
</DockPanel>
</Border>

This XAML document creates an object hierarchy as shown in Figure 3-1 and the display shown in Figure 3-2.

click to expand
Figure 3-1: An example XAML page object model

click to expand
Figure 3-2: The display from the previous XAML

You can access much of the functionality of such objects using only markup. Using only markup, you can do any of the following:

  • Describe a hierarchical set of objects that the runtime will instantiate

  • Set object properties to values known statically

  • Set object properties to values retrieved from a data source

  • Cause changed property values to be stored back into the data source

  • Repeatedly change a property s value over time

  • Bind an event handler to an object s event

However, although you can create some amazing user interfaces using only markup, you can also access an element s functionality programmatically using the XAML object model. The object model allows you to manipulate every aspect of the elements on a page. It actually provides additional capabilities that are not accessible through XAML.

Every XAML element derives from System.Windows.UIElement or System.Windows.ContentElement , and therefore all elements possess a number of common features. Elements can be grouped in the following four basic categories:

  • Controls derive from System.Windows.Control and handle user interaction.

  • Panels are specialized controls that derive from System.Windows.Panel and handle page layout and act as containers for elements.

  • Text formatting elements derive from System.Windows.TextElement and handle text formatting and document structure.

  • Shapes handle vector graphic shapes.