入门7-XAML语法简说

来源:互联网 发布:美工工作职责 编辑:程序博客网 时间:2024/05/02 20:20

今天对XAML的语法进行一下探讨.

先介绍一下常用的Silverlight元素对象(之前的入门中1-7中都有介绍)
========================
Canvas
Textblock
Glyphs
Rectangle
Ellipse
Line
Path
Polygon
POlyline
Image
MediaElement
========================

总结入门中1-7中的Code,我们可以总结出一个标准的模板:
========================
<Canvas xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    #####################################################
    #                                                   #
    #                                                   #
    #在此声明各种Silverlight元素对象,及注释            #
    #                                                   #
    #                                                   #
    #####################################################
</Canvas>
========================

从上可以看出,在XAML中的根元素(root element)是Canvas,由<Canvas>...</Canvas>来声明,然后在<Canvas>...</Canvas>来声明其他的

UIElement元素对象。Silverlight1.0 SDK帮助中对Canvas的定义如下:
=================================
In Silverlight 1.0, a Canvas is typically the root element for the XAML that is the Source of a Silverlight plug-in.
=================================
这里说的就是"Canvas是Silverlight plug-in中的XAML的根元素"。

模板中我们可以看到,xmlns="http://schemas.microsoft.com/client/2007",xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

这两个声明语句,这是什么意思呢?
首先我解释一下xmlns="http://schemas.microsoft.com/client/2007", 这里使用xmlns来声明Silverlight的命名空间;
而xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml则是使用xmlns:x来声明XAML的命名空间;

Silverlight1.0 SDK帮助中对Name属性和x:Name属性给出如下的解释;
=========================================================
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 should 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.
=========================================================

这里我不做过多的解释,只引用《Silverlight完美入门》书中的话:
你定义的XAML中,至少要声明xmlns="http://schemas.microsoft.com/client/2007"命名空间,这样你的Silverlight才可以运行否则就会出错;对于xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml这个命名空间,许多场合都会用到,所以最好也一起加入。


下面下面结合一段代码示例谈谈代码规范:
========================
<Canvas xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Canvas>
        <!--声明一个矩形-->
        <Rectangle>
            ...
        </Rectangle>
    </Canvas>
</Canvas>
========================
如上所示,
1 声明子元素时,其开头的位置应比父元素靠后四个空格
2 元素对象内的属性可以写成一行,也可以写成多行,不过写成多行,这样可读性更强,写成多行时注意,属性值的开头比本元素标签(tag)靠

后四个空格
3 定义对象前,要对定义的对象进行注释说明,比如定义一个什么颜色的矩形,是直角的还是圆角的,这些都需要在注释中说明,注释用法如下:<!--内容-->
4 在你加入其他元素对象前,最好先加入一个Canvas画布对象作为管理,如上:
    <Canvas>
        <!--声明一个矩形-->
        <Rectangle>
            ...
        </Rectangle>
    </Canvas>
这里定义Rectangle前就定义了一个Canvas对象


XAML也有自己的一些特性:
1 XAML元素对象中的tag必定对应于一个.NET Framework类
2 XAML是面向对象的
3 XAML是区分大小写的(Case-Sensitive)
4 XAML属性值是强类型的,因此类型要绝对匹配,例如Rectangle的Height属性定义为double数值,所以你要定义Height="abc",运行时就会报错
5 XAML会建立起自己的Element Tree,类似于HTML中的DOM Tree


对于Canvas元素对象,这里也简要的说一下:
其常用的属性,如下
Height,Width,Background,Opacity,Visibility,Canvas.Left,和Canvas.Top
其中,Canvas.Left,和Canvas.Top是定义其显示位置的,Height和Width是定义其显示大小的,Background定义其背景颜色,Opacity定义其透明度,Visibility定义其可见性,以下情况可导致Canvas元素对象不能显示出来:
=======================================
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.
=======================================

即:
Height="0"
Width="0"
Background="null"
Opacity="0"
Visibility="Collapse"

                                                                                                                 锐意进取 大胆创新
                                                                                                                      蒙哥马利 朱

                 

 
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 房间西晒窗帘不遮光怎么办 新建房屋一面墙体有裂缝怎么办 卫生间地砖缝隙出现渗水怎么办 西户窗户太晒怎么办 西晒的墙面很烫怎么办 儿童房颜色太粉了怎么办? 小孩在家里偷钱怎么办 脾气不好的猫该怎么办 二年孩子偷钱怎么办 孩子偷钱2000报警怎么办? 我儿子十岁老是偷钱怎么办 13孩子偷同学钱怎么办 孩子偷同学的钱怎么办 儿子十四岁了老偷钱怎么办 发现初中生的儿子偷钱怎么办 被亲戚怀疑儿子偷钱怎么办 房门选的太白了怎么办 大厅地砖颜色比墙砖浅怎么办 房屋外墙渗水物业不管怎么办 走丢了怎么办教学反思 托班教案迷路了怎么办 大班安全教案遇到小偷怎么办 小班孩子舞台表演找不到位置怎么办 懂你英语学完了怎么办 小班社会走丢了怎么办 帮小老鼠搬鸡蛋怎么办 小老鼠还能怎么办鸡蛋 中班教案走丢了怎么办 走丢了怎么办可后反思 社会走丢了怎么办教案 孩子一直不吃幼儿园的饭怎么办 大班安全游泳抽筋怎么办反思 汤洒了怎么办教学反思 迷路了怎么办小班详案 大班牙又痛又摇怎么办 大班安全教案着火了怎么办 小班社会生病了怎么办反思 脚扭伤了怎么办的反思 中班安全迷了眼怎么办 15个月的小孩长泡疹怎么办 墨盒加错颜色墨水怎么办