Canvas>
Grid>
UserControl> The code for the UserControl and the Grid is boilerplate Silverlight code produced by the IDE when any new Silverlight project is created. I’ve added only three items:l A Canvas called myCanvas which has an event called StartTimer that is called when the Canvas is first loaded l A blue and red Rectangle control that is 25 pixels square l An Image control which is also 25 pixels square. It is referenced inside the program as myImageUserControl和Grid的代码是当任何Silverlight项目被创建的时候都会由集成开发环境(IDE)生成的样板Silverlight代码。我只添加了三项:l 一个被命名为myCanvas的Canvas,它有一个叫做StartTimer的事件处理方法,这个方法将在Canvas被第一次加载时调用。l 一个25像素大小、红色边框、蓝色背景的正方形的矩形控件。l 一个同样25像素大小的正方形的图片控件,它在程序中以myImage引用。The XAML puts both controls in the upper left corner of the Canvas, which means that in design mode one will be hidden behind the other. The C# code I show in the next section moves the Rectangle off to the right of the Canvas, so that it has a unique location at run time.XAML代码将两个控件都放到了Canvas的左上角,也就是说,在设计模式下,其中一个控件将被隐藏到另外一个的后面。下一小节我展示的C#代码将矩形控件移动到了Canvas的右边,以便它在运行时有一个独立的位置。The TimerAnimations of the kind shown here are usually run off a timer which produces a kind of miniature application loop that is called at regular intervals and which acts as engine to drive the animation forward. The loop is started when the Canvas is loaded:计时器(Timer)这里展示的这种动画通常基于一个计时器(timer)运行,这个计时器以有规律的间隔调用一种小型程序循环,这种小型程序循环担当引擎来驱动动画向前播放。这个循环在Canvas被加载时启动:private void StartTimer(object sender, RoutedEventArgs e) { System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer(); // Call the timer once every 10 milliseconds myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10); myDispatcherTimer.Tick += new EventHandler(MoveShapes); myDispatcherTimer.Start(); }This code is pulled nearly verbatim from the Silverlight documentation. It begins by creating a timer, then asks that the timer be fired once every ten milliseconds. A method called MoveShapes will be called whenever the timer is fired. Finally the code Starts the timer.Here is the MoveShapes method, which is called by the timer once every ten milliseconds:这段代码是从Silverlight文档中几乎一字不差地摘录下来的。它以创建一个计时器(timer)开始,随后让计时器每10毫秒触发一次。每次计时器被触发,一个叫做MoveShapes的方法就会被调用。这是MoveShapes方法的代码,它每10毫秒就被计时器调用一次:Double p_w_picpathX = 0.0;
Double p_w_picpathY = 0.0;
Double rectX = 275.0;
Double rectY = 1.0;
Double incValue = 1.0;public void MoveShapes(object o, EventArgs sender)
{
p_w_picpathX += incValue;
p_w_picpathY += incValue;
rectX -= incValue;
rectY += incValue;
myRect.SetValue(Canvas.TopProperty, rectY);
myImage.SetValue(Canvas.TopProperty, p_w_picpathY);
}This method is passed two parameters. The first is copy of the timer. You can use this object to disable or change the traits of the timer.这个方法被传递进两个参数。第一个参数是计时器(timer)实例的引用。你可以使用这个对象来禁用计时器或者改变计时器的行为。This code in the MoveShapes covers several lines, but its structure is very simple. I’ve declared values to track the current X and Y location of the upper left hand corner of the Image and Rectangle controls. Note that I initialize the rectX field to 275.0. It is this value that moves the Rectangle control over to he right of the Canvas at program start up, so that it is no longer hidden behind Image control. Needless to say, in the next article in this series I will create separate objects for each sprite. At this stage however, I’m focusing on showing you the basic animation techniques in the least possible amount of code.MoveShapes方法的代码有好几行,但是它的结构却非常简单。我声明了一些值来跟踪图片控件和矩形控件左上角顶点的当前X和Y坐标值。注意,我将rectX字段的值初始化为275.0。这个值也正是在程序启动时矩形控件被移动到Canvas右边的初始位置,以便它不再被隐藏在图片控件之后。无需多说,在这个系列的下一篇文章,我将为每个精灵创建单独的对象。然而在现阶段,我将集中精力用尽可能少的代码向您展示基本的动画技术。The last lines show how to call the SetValue method of the Rectangle and Image control in order to move the control across the surface of the Canvas. Silverlight provides us with a nice bonus feature here by automatically handling this transformation. In particular, it erases the original p_w_picpath of the control and then repaints it in the new location. In most computer languages, I would have had to manually write the code to erase the old p_w_picpath.最后几行代码演示了如何调用矩形控件和图片控件的SetValue方法来移动控件穿过Canvas表面。在这里Silverlight通过自动处理这种转换为我们提供了一个很棒的优秀特性。尤其是,它擦除控件的原有图像然后再在新位置重新绘制图像。在大部分计算机编程语言中,我通常都不得不手工编写代码来擦除原有的图像。Note that the MoveShapes method begins by incrementing or decrementing the fields that specify the location of the controls. This allows us to define how we move the controls across the surface of the Canvas. It is, however, the call to SetValue that actually transforms the location of the controls. The end result is a smooth animation of the controls across the surface of their container.注意,MoveShapes方法通过增加或减小指定控件位置的字段的值来开始运行的。这允许我们定义怎样移动控件穿过Canvas表面。然而,是对SetValue这个方法的调用实际变换了控件的位置。结果就是产生了这些控件平滑地穿过他们容器表面的动画效果。The best way to see this animation in action is to download the code and try it yourself. Remember that you need to first install the Silverlight integration for Visual Studio 2008, as described in the Get Started section of the Silverlight web site.查看这段代码的实际动画效果最好的方式是您下载代码并亲自试试。记住,您首先需要安装Visual Studio 2008的Silverlight集成扩展,正如Silverlight官方网站的入门(Get Started)部分中所描述的一样。SummaryThe technique for animating controls that I’ve described in this article is very easy to use. Silverlight provides all the tools we need to set up simple animation without requiring any real effort on our part. As a result we can focus our energy on simply describing the path that we want our controls to follow. In particular, you need only:l Set up a timer that calls a method you define at discreet intervals. l Define the method called by the timer, and ensure that it contains logic for moving your controls.总结我在这边文章中所叙述的控件动画技术非常易于使用。Silverlight提供了所有我们需要的工具来创建简单动画,而不需要我们这边付出太多努力。因此我们可以把精力集中在简单明了地描述我们想让控件运动的路径。尤其是,你只需:l 设置一个计时器,以谨慎设定的时间间隔调用您预先定义的一个方法。l 实现被计时器调用的那个方法,确保它包含移动你的控件的逻辑。As mentioned above, I’ll follow this post with at least one additional article that describes additional techniques for animating sprites. Those techniques require us to write a bit more code than that shown here, but at heart they are also very simple and easy to understand.Download the SilverlightAnimatedTimer01.zip file containing the sample code from the LINQ Farm.正如上文提到的,我会紧随着这篇随笔再发表至少一篇额外的文章描述使精灵产生动画效果的附加技术。这些技术相比这里展示的需要我们再多写一些代码。但凭心而论,它们也很简单并且易于理解。可以从“LINQ Farm”上下载包含示例代码的SilverlightAnimatedTimer01.zip文件。REF:http://www.cnblogs.com/Ricky81317/archive/2009/06/05/1497029.html