XAML
In Silverlight, development is really split between the design and
the code. The design is accomplished using a markup language called XAML
(rhymes with camel). XAML (or eXtensible Application Markup Language)
is an XML-based language for representing the look and feel of your
applications. Since XAML is XML-based, the design consists of a
hierarchy of elements that describe the design. At its most basic level,
XAML can be used to represent the objects that describe the look and
feel of an application. These objects are represented by XML elements, like so:
<Rectangle />
<!-- or -->
<TextBox />
You can modify these XML elements by setting attributes to change the objects:
<Rectangle Fill="Blue" />
<!-- or -->
<TextBox Text="Hello World" />
Containers in XAML use XML nesting to imply ownership (a parent-child relationship):
<Grid>
<Rectangle Fill="Blue" />
<TextBox Text="Hello World" />
</Grid>
Using this simple XML-based syntax you can create complex, compelling
designs for your phone applications. With this knowledge in hand, we
can make subtle changes to the XAML supplied to us from the template. We
could modify the XAML directly, but instead we will start by using the
Visual Studio designer for the phone. In the main editor pane of Visual
Studio the MainPage.xaml file is split between the designer and the text
editor for the XAML. The left pane of the MainPage.xaml file is not
just a preview but a fully usable editor. For example, if you click on
the area containing the words “page name” on the design surface, it will
select that element in the XAML, as shown in Figure 8.
Figure 8. Using the Visual Studio XAML design surface
Once you have that element selected in the designer, the properties
for the element are shown in the Properties window (which shows up below
the Solution Explorer). If the window is not visible, you can enable it
in the View menu by selecting “Properties window” or by pressing the F4
key. This window is shown in Figure 9.
Figure 9. Location of the Properties window
The Properties window consists of a number of small parts containing a variety of information, as shown in Figure 10.
Figure 10. Contents of the Properties window
The section near the top (#1 in Figure 10) shows the type of object you have selected (in this example, a TextBlock
)
and the name of the object, if any (here, “PageTitle”). This should
help you ensure that you have selected the right object to edit its
properties. The next section down (#2) contains a Search bar where you
can search for properties by name, as well as buttons for sorting and
grouping the properties. The third section (#3) is a list of the
properties that you can edit.
From the Properties window you can change the properties of the selected item. For example, to change the text that is in the TextBlock
, you can simply type in a new value for the Text
property. If you enter “hello world” in the Text
property and press Return, the designer will change to display the new
value. Changing this property actually changes the XAML in the
MainPage.xaml file. The design surface is simply reacting to the change
in the XAML. If you look at the XAML the change has been affected there
as well, as shown in Figure 11.
Figure 11. The changed property
You can edit the XAML directly as well if you prefer. If you click on the TextBlock
above the PageTitle
(the one labeled “ApplicationTitle”), you can edit the Text
attribute directly. Try changing it to “MY FIRST WINDOWS PHONE APP” to
see how it affects the designer and the Properties window:
...
<TextBlock x:Name="ApplicationTitle"
Text="MY FIRST WINDOWS PHONE APP"
Style="{StaticResource PhoneTextNormalStyle}" />
...
Depending on their comfort level, some developers will find it easier
to use the Properties window while others will be more at ease editing
the XAML directly. There is no wrong way to do this.
Although the Visual Studio XAML designer can
create interesting designs, the real powerhouse tool for designers and
developers is Blend. Let’s use it to edit our design into something
useful for our users.