入门7-XAML语法简说

今天对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"

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

                 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值