当前位置: 移动技术网 > IT编程>网页制作>XML > SixSix翻译的XAML教程 语法简介

SixSix翻译的XAML教程 语法简介

2017年12月01日  | 移动技术网IT编程  | 我要评论
[this topic is pre-release documentation and is sub
[this topic is pre-release documentation and is subject to change in future releases. blank topics are included as placeholders.] 
xaml 语法简介

本文介绍了如何使用几种不同的方法在xaml中创建对象和设置他们的属性。

主题包括以下几个部分:
什么是xaml?  
声明对象 
设置属性 
其他相关主题
什么是xaml

xaml是一种陈述性语言。你可以使用xaml标记创建可视化的ui原件。 之后,你可以在一个单独的文件中使用javasscript来操作你在xaml所声明的对象、响应一些事件。作为一种以xml为基础的陈述性语言,它创建界面时,从原型到产品的过程非常直观,尤其是对于有网页设计背景知识和技术的人。

xaml文件通常是以.xaml为后缀的xml文件。下面是一个典型的silverlight xaml文件例子。.xaml
<canvas
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <rectangle
    width="100"
    height="100"
    fill="blue" />
</canvas>

声明对象

在xaml中,有以下几种方式声明对象和设置它们的属性::
object element syntax: 使用开放的和封闭的标签来声明对象,就像xml一样。你可以使用这种方法来声明根元素和设置它们的属性值。 
attribute syntax: 使用内嵌来声明对象。你可以使用这种方法来设置一个属性的值。. 
object element syntax

一种典型的使用object element syntax来声明对象的方法。.首先你要创建两个xml元素标签:
<objectname> 

</objectname> 


... objectname 是你想要实例化的对象的名字。下面的例子使用object element syntax声明一个canvas。xaml
<canvas>
</canvas>


一些对象, 比如canvas, 可以包含其他对象。.xaml
<canvas>
  <rectangle>
  </rectangle>
</canvas>


为了方便,如果一个对象里不包含其他对象,那么可以只使用一个标签来描述它xaml
<canvas>
  <rectangle />
</canvas>

使用attribute syntax声明对象
见下一部分, 设置属性, 获得更多有关attribute syntax的信息。
设置属性

使用 object element syntax,你可以在声明对象的时候设置它的属性. 在xaml中,有几种方法可以设置属性: 使用 attribute syntax, 或使用  property element syntax.
通过attribute syntax设置属性


<objectnameproperty="propertyvalue"> 

</objectname> 


... property 是属性名称,你会将propertyvalue 的值赋到它的身上。 下面的例子展示了如何使用attribute syntax 来设置一个rectangle的width, height, 和 fill .xaml
<canvas>
  <rectangle                                   
    width="100"height="100"fill="blue" />
</canvas>



使用 property element syntax设置属性

一些属性可以通过property element syntax来设置. 你通过创建xml elements来描述你想要的属性, 例如:
<objectname> 

  <objectname.property> 

    <propertyvalue ... /> 

  </objectname.property> 

</objectname> 


... property 是属性名称,你会将propertyvalue 的值赋到它的身上. 下面的例子展示了如何使用 property element syntax 来设置一个rectangle的fill ,使用a solidcolorbrush.xaml
<canvas>
  <rectangle
    width="100" 
    height="100"> 
    <rectangle.fill> 
      <solidcolorbrush /> 
    </rectangle.fill>
  </rectangle>
</canvas>

使用 content element syntax设置属性

有时候,当一个属性支持element syntax,你可以忽略属性名,直接将属性值内嵌在对象标签里。这就是content element syntax. 下面的例子展示了怎样不指定 text 属性,设置textblock的 text 属性值 。xaml
    <textblock>
      hello!
    </textblock>

使用 implicit collection syntax设置属性

有时候, 一个属性表现为一个集合, 你可以忽略集合名字,直接设置属性值。这就是implicit collection syntax.。下面的例子展示了对于lineargradientbrush 如何忽略gradientstopcollection ,以及直接指定 gradientstop 对象。 gradientstopcollection 包含在第一个lineargradientbrush中,,但在第二个里被忽略了。xaml
    <rectangle width="100" height="100"
      canvas.left="0" canvas.top="30">
      <rectangle.fill>
        <lineargradientbrush>
          <lineargradientbrush.gradientstops>

            <!-- here the gradientstopcollection tag is specified. -->
            <gradientstopcollection>
              <gradientstop offset="0.0" color="red" />
              <gradientstop offset="1.0" color="blue" />
            </gradientstopcollection>
          </lineargradientbrush.gradientstops>
        </lineargradientbrush>
      </rectangle.fill>  
    </rectangle>

    

    <rectangle width="100" height="100"
      canvas.left="100" canvas.top="30">
      <rectangle.fill>
        <lineargradientbrush>
          <lineargradientbrush.gradientstops>

            <!-- notice that the gradientstopcollection tag
                 is omitted. -->
            <gradientstop offset="0.0" color="red" />
            <gradientstop offset="1.0" color="blue" />
          </lineargradientbrush.gradientstops>
        </lineargradientbrush>
      </rectangle.fill>  
    </rectangle> 


有时你甚至可以同时忽略集合元素标签和属性元素标签::xaml
    <rectangle width="100" height="100"
      canvas.left="200" canvas.top="30">
      <rectangle.fill>
        <lineargradientbrush>
          <gradientstop offset="0.0" color="red" />
          <gradientstop offset="1.0" color="blue" />
        </lineargradientbrush>
      </rectangle.fill>  
    </rectangle> 

什么时候使用attribute或property element syntax设置属性

所有属性都支持attribute 或property element syntax, 一些属性支持其他方法. 设置属性所支持的方法取决于属性值所认可的对象类型。.
如果属性值是简单类型, 比如 double, integer,string, 这种属性只支持 attribute syntax . 下面的例子展示了如何使用 attribute syntax 设置rectangle的width.width属性支持attribute syntax,因为他的属性值是double类型。 xaml
<rectangle width="100" />

是否可以使用attribute syntax取决于你使用于设置属性的对象是否支持attribute syntax.下面的例子展示了使用 attribute syntax 设置一个rectangle的 fill属性。在你使用solidcolorbrush去设置fill属性的时候,它是支持attribute syntax的,因为solidcolorbrush支持attribute syntax. xaml
<rectangle fill="blue" />

是否能够使用element syntax 设置属性取决于你使用的对象是否支持。如果对象支持object element syntax,属性才支持property element syntax 。下面的例子展示了使用property element syntax 设置一个rectangle的fill.当你使用solidcolrbrush设置fill的时候,它是支持attribute syntax的,因为solidcolorbrush支持attribute syntax 。. xaml
<rectangle>
  <rectangle.fill>
    <solidcolorbrush />
  </rectangle.fill>                                   
</rectangle>

see also 
silverlight object models

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网