今天对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"
锐意进取 大胆创新 蒙哥马利 朱
总结以上是内存溢出为你收集整理的入门7-XAML语法简说全部内容,希望文章能够帮你解决入门7-XAML语法简说所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)