First Program :
We begin with a simple example that displays the classic "Hi World!". You have already created a Silverlight project as the previous chapter, you have an HTML page, mypage.html, and the necessary files in the directory of your project.
1) Create a canvas
The XAML content to fit within the guidelines Canvas in Silverlight environment:
Code:
<Canvas
xmlns = "http://schemas.microsoft.com/client/2007"
xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml">
</ Canvas>
2) Add a text block
The TextBlock tag allows you to place text in the canvas:
Code:
<TextBlock>
Hello World!
</ TextBlock>
3) Define the attributes
You can choose the font, size, color and other text attributes.
Code:
<TextBlock
FontFamily = "Verdana"
FontSize = "20">
Hello World!
"/ TextBlock>
4) The complete XAML
Code:
<Canvas
xmlns = "http://schemas.microsoft.com/client/2007"
xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml">
<TextBlock
FontFamily = "Verdana"
FontSize = "29">
Hello World!
</ TextBlock>
</ Canvas>
5) Show page
You can now view the page mypage.html with a browser, Internet Explorer, Firefox or another.
The Syntax of XAML :
XML syntax
XAML is a markup language derived from XML. The widgets are defined by tags open or closed, feature attributes.
Example of tag with content:
Code:
<TextBlock> A text </ TextBlock>
and without content:
The attributes are as variable assigned a value. In XML these values are put in quotes, as opposed to content that is either a text or one or more other tags.For example we give the name t1 with the Name attribute:
Code:
<TextBlock Name="t1" /> <TextBlock Name="t1"> A text </ TextBlock>
We will see that the tags can contain other tags, and even attributes can become beacons or vice versa, for example:
Code:
<TextBlock Text="some text />
Syntax properties :
An object properties, by which we mean that characterizes it, can be written as an attribute. For example, the background color property of a rectangle is written with the Fill attribute:
Code:
<Rectangle Fill="Red" />
In order to describe complex properties, XAML provides a format alternative called "Property element syntax (syntax element property), which extends the syntax of XML and gives new meaning to the point. In XML, the value of an attribute must be a string. In XAML, this can be another object of language. But it does not assign direct object to the attribute with the equal sign, is associated with a point by its own syntax for XAML that has the form:
Let's take the example of the Rectangle object and the Fill property is the fill color, becomes the attribute tags:
Code:
<Rectangle> <Rectangle.Fill> </ Rectangle.Fill> </ Button>
This lets you add tags and attributes to the Fill property, such as a texture made with a picture, what we will see later in this manual. Another example is provided by the language specification, one of which is assigned a button-down list:
Code:
<button> <Button.ContextMenu> <ContextMenu> <menuitem> Open </ MenuItem> <menuitem> Close </ MenuItem> </ ContextMenu> </ Button.ContextMenu> </ Button>
We see that ContextMenu, which is a menu list, becomes property of button with "Button.ContextMenu," and expresses Within the description of the button. The contents of a tag can be seen as property. Thus, the button text is a property which is written in such content or attribute value or text Content by subject:
Code:
<TextBlock Text="Some Text" />
Namespaces :
Namespaces are specificities as attributes of the container as overall file XAML Canvas or Window, or Page. These are predefined URL that will be given in the examples which correspond to the type of XAML definition.
Example:
Code:
<canvas Xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
For namespaces other than the default space (first line), the prefix (as x above) must precede each element of that namespace. In this case we will:
For each element of the namespace x.
Properties attached :
It is a concept for XAML. The syntax is the same as the elements of property seen above, connects one property name to a type name (rather than an element name such as Button).
The goal is to add properties to a type. The elements of this type can then have the properties so defined.
Events attached :
In XAML, you can define an event by type, while the handlers are attached to objects represented by tags (such as Button). The syntax is always the same:
Type.environment
Extensions :
It is possible to extend the XAML using a special syntax: () is placed between the extension, consisting of a class name followed by the body.
Example from the language specification:
Code:
<TextBlock Style="{StaticResource Style}"> A text </ TextBlock>
The class contains definitions StaticResource added, and the body myStyle becomes a property of a button. We may use Button.Style in the definition of the button and take advantage of new opportunities implemented in the classroom.
Characteristic :
Sensitivity :
XAML is case sensitive. The initials of the words in capital letters must be preserved. This does not necessarily apply to attribute values, and true and true are eligible, if the parser recognizes them.
Whitespace :
The extra spaces are ignored, and special characters such as the code tab, equivalent to a space.
Root tag :
Like any XML document, a XAML definition must be enclosed within a single tag, called the root element. WPF for a page, the container is the tag page. For Silverlight, it is Canvas. For a local application is Windows.
Gadgets For SilverLight
Bookmarks