Today we will discuss the syntax of XAML.
First, we will introduce common Silverlight element objects (which are described in 1-7 of the previous tutorials)
======================================
Canvas
Textblock
Glyphs
Rectangle
Ellipse
Line
Path
Polygon
Polyline
Image
Mediaelement
======================================
To sum up the code in step 1-7, we can summarize a standard template:
======================================
<Canvas xmlns = "http://schemas.microsoft.com/client/2007"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml">
######################################## #############
##
##
# Declare various Silverlight element objects and comments here #
##
##
######################################## #############
</Canvas>
======================================
As shown in the preceding figure, the root element in XAML is canvas, which is composed of <canvas>... </canvas>, and then in <canvas>... </canvas> to declare other
Uielement object. The definition of canvas in the help of silverlight1.0 SDK is as follows:
======================================
In Silverlight 1.0, a canvas is typically the root element for the XAML that is the source of a Silverlight plug-in.
======================================
"Canvas is the root element of the XAML in Silverlight plug-in ".
As we can see in the template, xmlns = "http://schemas.microsoft.com/client/2007", xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
What do these two statements mean?
First I will explain xmlns = "http://schemas.microsoft.com/client/2007", here I use xmlns to declare the Silverlight namespace;
Xmlns: x = "struct;
The silverlight1.0 SDK provides the following explanations for the name and X: Name attributes;
========================================================== ========================
The Name property is generally equivalent to the X: Name attribute specified in XAML. Name and X: Name differ in the following aspects:
1 x: name as an attribute is understood by all XAML readers. name as an attribute in the default xmlns is only understood if it maps to an existing property on the element where it is declared. in the case of dependencyobject. name, all common elements used for UI definition will possess the name property/attribute through dependencyobject. dependencyobject provides core support for all Silverlight classes that can be declared as object elements in XAML, so this XAML language versus XAML implementation distinction is not relevant for Silverlight XAML. however, you shoshould keep this distinction in mind if you are importing XAML that may have served as the UI definition for related XAML-consuming technologies (such as WPF ).
2 markup attributes are by nature write-only, unless you are examining the markup with a dedicated xml dom, so the only operation you can perform with X: Name is to set the attribute value.
3 using X: name will require that you map the XAML xmlns to the X: prefix. this is typically done initially in most XAML files that are generated by tools, templates, or visual editors. the prefix X: is used by convention for the XAML xmlns; you can choose a different prefix that you will map to the XAML xmlns if you wish.
========================================================== ========================
I will not explain it too much here. I will only reference the words in the Silverlight perfect getting started book:
In your defined XAML, at least the xmlns = "http://schemas.microsoft.com/client/2007" namespace should be declared so that your Silverlight can run or else errors will occur; For xmlns: x = "struct.
The following uses a sample code to describe code specifications:
======================================
<Canvas xmlns = "http://schemas.microsoft.com/client/2007"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas>
<! -- Declare a rectangle -->
<Rectangle>
...
</Rectangle>
</Canvas>
</Canvas>
======================================
As shown above,
1. When declaring a child element, it must start with four spaces behind the parent Element
2. attributes in an element object can be written as one row or multiple rows, but multiple rows can be written. This is more readable. Note that when writing multiple rows, the attribute value starts
Last four spaces
3. Before defining an object, comment on the defined object. For example, to define a rectangle of the color, right angle, or rounded corner, you must describe it in the comment, the annotation usage is as follows: <! -- Content -->
4. Before you add other element objects, you 'd better add a canvas object for management, as shown above:
<Canvas>
<! -- Declare a rectangle -->
<Rectangle>
...
</Rectangle>
</Canvas>
A canvas object is defined before rectangle.
XAML also has its own features:
1 The tag in the XAML element object must correspond to a. NET Framework class.
2 XAML is object-oriented
3. XAML is case-sensitive)
4. The XAML attribute value is strongly typed, so the type must be absolutely matched. For example, if the height attribute of rectangle is defined as a double value, you must define Height = "ABC" and an error will be reported during running.
5. XAML creates its own element tree, which is similar to the DOM tree in HTML.
For the Canvas Element Object, here is a brief introduction:
Its common attributes are as follows:
Height, width, background, opacity, visibility, canvas. Left, and canvas. Top
Canvas. left, and canvas. top defines its display position, height and width defines its display size, and background defines its background color. Opacity defines its transparency and visibility defines its visibility, in the following cases, the Canvas Element object cannot be displayed:
========================================================
The height property is equal to 0.
The width property is equal to 0.
The background property is equal to null.
The opacity property is equal to 0.
The visibility property is equal to collapsed.
One of the ancestors of the canvas object is not visible.
========================================================
That is:
Height = "0"
Width = "0"
Background = "null"
Opacity = "0"
Visibility = "collapse"
Forge ahead, boldly innovate
Montgomery Zhu