初学SilverLight---XAML简介

    初来咋到的学习SilverLight,由于最近有个培训,也就只有认真的研究下这个东西了。看了下示例的效果,确实很不错,感觉自己已经很落伍了啊,呵呵。
    转载一个译文,给像我一样的初学silverLight的朋友。
    什么是XAML? 
    XAML是一种陈述性语言。你可以使用XAML标记创建可视化的UI原件。 之后,你可以在一个单独的文件中使用JavasScript来操作你在XAML所声明的对象、响应一些事件。作为一种以XML为基础的陈述性语言,它创建界面时,从原型到产品的过程非常直观,尤其是对于有网页设计背景知识和技术的人。XAML文件通常是以.xaml为后缀的XML文件。下面是一个典型的Silverlight 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。

<Canvas>
</Canvas>

一些对象, 比如Canvas, 可以包含其他对象。.

<Canvas>
  
<Rectangle>
  
</Rectangle>
</Canvas>

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

<Canvas>
  
<Rectangle />
</Canvas>

 

    使用Attribute syntax声明对象

见下一部分, 设置属性, 获得更多有关attribute syntax的信息。

    设置属性

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

    通过Attribute syntax设置属性

<object Nameproperty="propertyValue">

</objectName>

... property 是属性名称,你会将propertyValue 的值赋到它的身上。 下面的例子展示了如何使用attribute syntax 来设置一个Rectangle的Width, Height, 和 Fill .

 

<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.

 

<Canvas>
  
<Rectangle
    
Width="100" 
    Height
="100"> 
    
<Rectangle.Fill> 
      
<SolidColorBrush /> 
    
</Rectangle.Fill>
  
</Rectangle>
</Canvas>

 

    使用 Content Element Syntax设置属性

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

 

<TextBlock>
      Hello!
</TextBlock>

 

    使用 Implicit Collection Syntax设置属性

有时候, 一个属性表现为一个集合, 你可以忽略集合名字,直接设置属性值。这就是implicit collection syntax.。下面的例子展示了对于LinearGradientBrush 如何忽略GradientStopCollection ,以及直接指定 GradientStop 对象。 GradientStopCollection 包含在第一个LinearGradientBrush中,,但在第二个里被忽略了。

 

<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>

有时你甚至可以同时忽略集合元素标签和属性元素标签:

<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 。

<Rectangle>
  
<Rectangle.Fill>
    
<SolidColorBrush />
  
</Rectangle.Fill>            
</Rectangle>

 

 


 

posted @ 2007-12-28 16:44  一瞬间  阅读(509)  评论(3编辑  收藏  举报