Using Text in XAML
XAML is an XML dialect, you'll see a lot of angle brackets here and throughout this book. In this chapter, we will have a look at the most important XAML elements. It is virtually impossible to cover them all in a book of this size, but we will present as many as possible to let you dive into XAML with maximum speed and get a sense of the power of Silverlight markup.
If you have already worked with XAML for WPF applications, you already know most of what is covered in this chapter . However, there are some subtle differences: Silverlight does not support the full XAML format of WPF does, but only a (quite decent) subset. Future releases of Silverlight will increase the percentage of supported WPF elements and attributes, but some things just cannot work in a web browser as they do in a desktop application.
<Canvas xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">If you are using the Silverlight Project template, the original structure looks different:
...
</Canvas>
<UserControl x:Class="HelloWorld.Page"To facilitate a migration to the script template, most examples will use as the first element, either in the form of the root element (script template) or as the first element within (project template). Whatever way you prefer, always remember to put the correct namespaces at the beginning.
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
...
</Grid>
</UserControl>
Using Text
The first example used to introduce most technologies is some variation of "Hello World" . The element used for this is, and there are two ways to provide this text:
Within the Text attribute of the element
As a text node within the element
Example 1 uses the latter approach to output some simple text. Note that using text within was not allowed in older Silverlight versions, but it worked nevertheless; Figure 1 proves that it still does.
Example 1. Using simple text, the XAML file (Page.xaml, project Text1)
If you are using the web site and do not want to rely on the Extensions, there is an alternative approach. First, you need JavaScript code that initializes the Silverlight content. The SDK and the script templates contain a file called Silverlight.js that provides you with the functionality to embed Silverlight content into an HTML page. The page hosting the Silverlight content uses the Silverlight.js file and calls one of its methods, Silverlight.createObjectEx(), as you can see in Example 2. For most of the remainder of this book, we will use the project-based template; however, it requires little effort to port these examples to the script web site template.
Example 2. Using simple text, the JavaScript code (excerpt from Default.html, project ScriptWeb)
Note the highlighted code elements:
The source property must be filled with the URL of the XAML file.
The parentElement property must be filled with a reference to the DOM element that will hold the Silverlight content.
The id property provides a value that JavaScript code may use to access the Silverlight content.
Now let's have a closer look at the HTML file that is used as the primary page to be loaded in the browser. The HTML page needs to contain a
container with the same ID that has been provided in the parentElement property. Finally, the page needs to include the JavaScript code from Example 2. 3 has the full code, and Figure1 shows the output—the text appears.Example 3. Using simple text, the HTML file (Default.html, project ScriptWeb)
NOTE
Creating new Silverlight apps starts with copying and pasting most of the time. When creating new content, you need copies of the HTML file, the XAML file, and, optionally, the XAML JavaScript file (you will see such a file in action in the following chapter). Then you just have to update all filenames and you are set. Therefore, we will print the HTML file only if it is beneficial to better understand the concept of a given example. We will also avoid reprinting the HTML file with the JavaScript code if there is no special additional information in it. The code downloads for this book always come with complete, running code.
Figure 1 shows the default layout for text: the text uses the Lucida font, has a size of 11 points, and is displayed in black. To make this possible, the font does not even have to be installed on the client (or on the server) it is part of the plug-in. Therefore, the experience on Mac OS X is almost the same, as Figure 2 shows.
Figure 1. The text is displayed
Figure 2. The same text on Mac OS X
Apart from the Lucida font, several other fonts are also supported cross-platform:
Arial
Arial Black
Comic Sans MS
Courier New
Georgia
Times New Roman
Trebuchet MS
Verdana
Other fonts, even if they are installed on the client, are not supported; Silverlight uses Lucida if the font name is invalid.
NOTE
It is possible to load external OpenType or TrueType (TTF) fonts and use them within a Silverlight application.
There are several ways to apply these fonts. First of all, some of the attributes come in handy:
FontFamily
-
The font family name (e.g., Arial).
FontSize
-
The font size in points (e.g., 12).
FontWeight
-
How to display the font (e.g., Thin, ExtraLight, Light, Normal, Medium, SemiBold, Bold, ExtraBold, Black, and ExtraBlack; unfortunately, IntelliSense provides you with additional, invalid choices).
Example 4 shows some styling options. It also introduces a new XAML element:
The <LineBreak> element
-
This element defines, well, a line break.
The Foreground attribute
-
This defines the foreground (here it is text) color. You can use a defined color name (Red, Green, Blue, etc.), or an RGB triple (#ff0000, #00ff00, #0000ff, etc.), or aRGB. The "a" stands for alphatransparency. Just provide a value between 0 (00) and 255 (ff) that defines the degree of the nontransparency. If you set it to 00, the element is fully transparent (e.g., the background is seen, the element is not). If you set it to ff, the element is not transparent at all, so you do not see the background. If you use a value in between, the background shines through at the given degree. For instance, #7fffff00 is a yellow (ffff00) that is about 50% transparent (7f is hex for 127).
NOTE
You can also provide the background color for an element, using the Background property.
Refer to Figure 3 for the output in the browser.