Silverlight控件相关操作技巧讲解

开发 开发工具
Silverlight控件的操作对于开发人员来说是一个非常重要的操作步骤。只有详细的掌握好各个控件的使用技巧,才能灵活的应用这款工具完成我们的功能需求。

Silverlight工具中内置了很多种控件。开发人员使用这些Silverlight控件可以轻松的完成界面图形的操作,以及一些音频视频的相关操作。Silverlight的界面是通过控件套用控件来改变页面的布局与实现体验者的视觉效果的。本文介绍Silverlight即时显示JavaScript程序的状态,详细介绍JavaScript实现对Silverlight的控件访问、修改、删除、创建。#t#

提示:对控件进行操作,如果在控件的onLoad之前访问和操作都会出现异常!,是因为找不到对象。

下面的Silverlight控件实例是JavaScript循环创建动画的效果,显示内容可以随意改变。控件的格式是XAML中定义的TextBlock(文本控件),包括Animation创建,添加,和删除等功能。

变量说明:

  1. var txtFormat=new _textBlock(); 
    //字体格式对象  
  2. var writeText="Welcom to WPF.COM!";
    //运画显示内容  
  3. var CanvasLeft=0
    //随环改变  
  4. var CanvasTop=0
    //加载的XAML中的高度位置  
  5. var charIndex=0
    //显示的字符索引  
  6. var split_width=15
    //每一个字符的宽度,可以对此智能改变  
  7. var _silverlight_c; 
    //指定的Silverlight控件对象 

在Page.xaml.js文件中创建了_textBlock类,使用此类记录textBlock中的格式,大小信息。

function _textBlock()  
{  
this.fontFamily="";  
this.width=0;  
this.height=0;  
this.textWrapping="Wrap";  
this.fontSize=0.0;  
}  
var txtFormat=new _textBlock(); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

Silverlight控件在onLoad事件中加载了XAML中的格式,并把样式控件删除,并且调用了显示动画的方法(goAnimation)。

handleLoad: function(control, 
userContext, rootElement)   
{   this.control = control;   _silverlight_c=control;   var tStyle=control.content.
findName("tStyle");  
for(var i in txtFormat)   {   txtFormat[i]=tStyle[i];   }   CanvasLeft=tStyle["Canvas.Left"];   CanvasTop=tStyle["Canvas.Top"];   control.content.root.children.
remove(tStyle);  
tStyle=null;   goAnimation();   }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

下面是goAnimation的代码:

function goAnimation(){  
if(charIndex<writeText.length)  
{  
var this_char=writeText.substr(charIndex,1);  
if(this_char!=" ")  
{  
var t_control=_silverlight_c.content.
createFromXaml('
<TextBlock xmlns:x="http://
schemas.microsoft.com/winfx/2006/xaml"
 
Text="Hello" x:Name="t'+charIndex+'"> <TextBlock.RenderTransform><TransformGroup> <ScaleTransform ScaleX="1" ScaleY="1"/> <SkewTransform AngleX="0" AngleY="0"/> <RotateTransform Angle="0"/> <TranslateTransform X="0" Y="0"/> </TransformGroup></TextBlock.RenderTransform> </TextBlock>');   CanvasLeft+=split_width;   for(var i in txtFormat){t_control[i]=txtFormat[i];}   t_control["Canvas.Top"]=-20;   t_control["Canvas.Left"]=CanvasLeft;   t_control.foreground="#ffffffff";   t_control.Text=this_char;   _silverlight_c.content.root.children.add(t_control);   var storyboard_str='<Storyboard xmlns:x="http://
schemas.microsoft.com/winfx/2006/xaml" 
x:Name="animation'
+charIndex+'">';   storyboard_str+='<DoubleAnimationUsingKeyFrames 
BeginTime="00:00:00" Storyboard.TargetName=
"t'+charIndex+'" Storyboard.TargetProperty=
"(UIElement.RenderTransform).(TransformGroup.
Children)[2].(RotateTransform.Angle)"
>';   storyboard_str+='<SplineDoubleKeyFrame 
KeySpline="0.091,0.532,1,1" KeyTime="00:00:00.
6000000"
 Value="-360"/>';   storyboard_str+="</DoubleAnimationUsingKeyFrames>";   storyboard_str+='<DoubleAnimationUsingKeyFrames 
BeginTime="00:00:00" Storyboard.TargetName=
"t'+charIndex+'" Storyboard.TargetProperty="
(UIElement.RenderTransform).(TransformGroup.
Children)[3].(TranslateTransform.Y)"
>';   storyboard_str+='<SplineDoubleKeyFrame 
KeySpline="0.091,0.532,1,1" KeyTime="00:00:00.
6000000"
 Value="'+CanvasTop+'"/>';   storyboard_str+='</DoubleAnimationUsingKeyFrames>';   storyboard_str+='<DoubleAnimationUsingKeyFrames 
BeginTime="00:00:00" Storyboard.TargetName=
"t'+charIndex+'" Storyboard.TargetProperty="
(UIElement.Opacity)"
>';   storyboard_str+='<SplineDoubleKeyFrame 
KeyTime="00:00:00" Value="0.055"/>';   storyboard_str+='<SplineDoubleKeyFrame 
KeyTime="00:00:00.6000000" Value="1"/>';   storyboard_str+='</DoubleAnimationUsingKeyFrames>';   storyboard_str+="</Storyboard>";    var storyboard_control=_silverlight_c.
content.createFromXaml(storyboard_str);  
t_control.Resources.add(storyboard_control);   storyboard_control.begin();   }   CanvasLeft+=split_width;   charIndex++;   setTimeout("goAnimation()",100);   }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.

 

使用content.createFromXaml方法创建Silverlight中的对象。

使用content.findName 利用x:Name查找名称对象

使用silverlight_control.children.add添加控件。

使用silverlight_control.Resources.add添加StoryBoard动画对象(Storyboard)。
storyboard_obj.Begin播放。

Silverlight控件的具体操作方法就为大家介绍到这里。

责任编辑:曹凯 来源: CSDN
相关推荐

2009-12-30 18:18:32

Silverlight

2009-12-31 16:44:53

Silverlight

2009-12-30 14:51:47

Silverlight

2009-12-30 17:29:53

Silverlight

2009-12-31 10:01:05

Silverlight

2009-12-30 13:30:16

Silverlight

2010-01-28 16:55:26

Android对话框

2009-12-30 18:07:54

Silverlight

2009-12-29 16:08:41

Silverlight

2009-12-30 11:16:36

Silverlight

2009-12-30 16:43:47

Silverlight

2009-12-31 10:21:53

Silverlight

2009-12-30 13:37:24

Silverlight

2009-12-30 10:15:57

Silverlight

2009-12-30 10:25:03

Silverlight

2009-12-31 16:38:19

Silverlight

2009-12-30 10:44:38

Silverlight

2009-12-31 11:15:57

Silverlight

2009-12-30 09:55:51

Silverlight

2009-12-31 11:35:20

Silverlight
点赞
收藏

51CTO技术栈公众号