Section 2.2. Defining XAML Applications


2.2. Defining XAML Applications

A XAML application comprises two types of elements: an application element and the set elements that make up the user interface. The XAML files contain the user-interface definition for your application. The codebehind files will contain the application logic and the code that handles event processing. XAML does not provide a mechanism for handling events, but it can direct the runtime engine to call event handlers written in C# or VB.NET. If you're a developer, you'll code the event handlers and application logic just as you always have, but because the user-interface code is separate, you'll have to pay a bit more attention to the names of the handlers and elements you reference because you don't define themthey're declared and named in the XAML file.

You can define XAML applications completely using C# or VB.NET. The CLR classes represented by XAML are all accessible through code, and you can write applications just as you always have, if you so desire. XAML offers you the ability to completely separate the presentation layer (user interface) from the application logic, thus making it easier to split up development responsibilities and isolate UI changes from the code. Appendix H provides an example of an application declared in XAML, as well as entirely in C#.


The most common application element is of type NavigationApplication. NavigationApplication defines an application that behaves like a web application or wizard in that it consists of pages between which a user navigates using hyperlinks and forward and back buttons.

The application definition is generally declared in its own file. It requires two properties to be set, the namespace and the startup URI, which is the URI of the first page that should be loaded when the application starts. For our purposes in this chapter, the application definition file will be called MyApp.xaml. It is detailed in Example 2-1.

Example 2-1. MyApp.xaml

     <NavigationWindow         xmlns="http://schemas.microsoft.com/winfx/avalon/2005"         StartupUri="Page1.xaml" /> 

In XAML, element names correspond to CLR object names, and attributes represent properties. The exception to this rule is with standard XML elements, such as xmlns, which is used to declare the namespace used within the XML file. The namespace used here is the default namespace for the application and identifies the Avalon types. If we did not specify the Avalon namespace as the default, all core XAML elements would need to include a reference to it. That's a lot of extra typing. It is much easier to use the Avalon namespace as the default, unless you will be primarily using custom elements defined in your own namespace, in which case, it is probably easier to specify your own namespace as the default and explicitly identify XAML elements instead. All the examples in this book will declare the Avalon namespace as the default. Every XAML element requires either explicit references to the namespace on a per-element basis or the declaration of the Avalon namespace as the default of the root element. Of course the latter is recommended, as it will alleviate the requirement to explicitly reference the namespace for every XAML element in the file.

The first element declared in any XAML file is called the root element. The root element must contain a reference to the namespace in which it is defined. For XAML elements, the namespace is http://schemas.microsoft.com/winfx/avalon/2005.

The default namespace will change when WPF officially ships.


Root elements are containers that hold other XAML elements. The most common root element for the application definition is NavigationWindow . The most common root elements for a page definition are Panel and its subclasses, DockPanel and StackPanel, and Page. Window is also used, though less often than the aforementioned elements.

In Example 2-1, the StartupUri attribute of the NavigationWindow specifies the XAML page that will be loaded when the application starts, in this case Page1.xaml.Additional attributes of NavigationWindow can be specified. For a complete description of NavigationWindow, see Chapter 8.

Page1.xaml will contain the actual definition for the user interface. Any subsequent pages will be referenced through allowable mechanisms, such as the HyperLink element. Like all XAML files, Page1.xaml requires a root element. The file is shown in Example 2-2.

Example 2-2. Page1.xaml

     <StackPanel xmlns="http://schemas.microsoft.com/winfx/avalon/2005">        <TextBlock>Hello World</TextBlock>        <Button Width="100">Click Me</Button>     </StackPanel> 

StackPanel is fully described in Chapter 7. Like DockPanel, it is used to hold elements, and that is all you need to know for now. The TextBlock element holds text, and the Button element represents a standard user-interface button. Interpreting the code in XamlPad produces the output shown in Figure 2-1.

This is an extremely simple example of a XAML application with absolutely no attention paid to style, layout, or usefulness. Refining these aspects of user-interface design is a subject for subsequent chapters. For now, it is only important that the file declares the minimum requirements for a XAML application. With a successfully defined application definition (MyApp.xaml) and a page definition (Page1.xaml), it's time to build the application into a Windows executable.

Figure 2-1. A simple XAML page previewed in XamlPad





XAML in a Nutshell
XAML in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596526733
EAN: 2147483647
Year: 2007
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net