Root Elements
Root elements are special derivations of panel elements. They serve as the fundamental containers for pages. Every page requires exactly one root element. In the next chapter, when we build an application with Visual Studio, you will notice that the default root element is Grid. It is not uncommon for other panel elements (e.g., StackPanel, DockPanel, or Canvas, as well as Page and Window) to serve as root elements. The root element must contain a reference to the namespace needed by the other elements in the container.
- Figure 2-5. My Window
You should be able to type the following code into XAMLPad and get a result similar to the Hello World text we showed previously:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Hello" Height="300" Width="300" >
<Grid Name="HelloWorldGrid"> <Label
Name="HelloWorldLabel" Content="Hello World" FontFamily="Verdana" FontSize="32pt"
This time we did not use a Window container, but rather a Page container. Your XAMLPad display should automatically have rendered "Hello World" in 32-point Verdana, as shown in Figure 2-6.
All XAML documents expect that the elements contained inside the root element be appropriately referenced via namespace declarations. We do this here by declaring the namespace(s) for our controls as attributes of the Page:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
If the provided elements are too rudimentary for you, you can create custom elements by deriving new classes from Page or Window and exposing them as XAML elements. Indeed, for very sophisticated user interactions where the standard features may not suffice, this is probably the desired course of action.
- Figure 2-6. Hello World
Post a comment