使用Silverlight实现 FLASH的动画效果

使用Silverlight实现 FLASH的动画效果,第1张

概述  Silverlight Animations with XAML & Code Before we start looking at animating our User Controls with code, let's take a few minutes to see how XAML based animations work. You can think of using XAML

 

Silverlight Animations with XAML & Code

Before we start looking at animating our User Controls with code,let's take a few minutes to see how XAML based animations work. You can think of using XAML Storyboards as the equivalent of doing your tweens on the timeline directly instead of Coding an onEnterFrame() or setInterval() event. In fact,a similar interface to the Flash timeline can be found in Expression Blend for creating Storyboards visually using the animation timeline and KeyFrames.

Here is the sample flash movIE we will be recreating in Silverlight using XAML storyboards and coded events.

The steps taken in Flash to create the above movIE were:

Create a new Flash Project with a script layer and single frame. Create a movIEClip named dot with its registration in the center. Export the dot MovIEClip for ActionScript. Add a KeyFrame in dot's timeline at frame 40. Shape tween over the next 20 frames to drop the dot and fade it out On frame 60,have dot remove itself with removeMovIEClip(this); Write some code in the script layer on the main stage like this:

This is a great example to duplicate in Silverlight to show how to mix Traditional XAML animations and procedural code. We'll use XAML to duplicate our timeline tweens in Flash and C# procedural code to duplicate our event handlers and dynamic visuals. Let's get started.

In Visual Studio,create a new Silverlight Project called Lesson02_a. After you accept the hosting option,you'll see App.xaml and Page.xaml with their respective code behind files ready to go. Since we will be creating our visual content in a separate User Control (just as dot isn't a visual on the stage but a MovIEClip defined in the Flash library),the only changes you need to make in Page.xaml for this project is to switch the default GrID tag to a Canvas tag and style the background color and size to whatever you wish. You will also want to add a Loaded event handler to Page.xaml's User Control tag up top and let Visual Studio create the function stub for you by pressing Tab while tyPing it. Your adjusted Page.xaml should Now look something like this:

Add a new User Control to our Silverlight Project (In the Solution Explorer,right click on Lesson02_a > Add > New Item > Silverlight User Control) and name it Dot.xaml.

As we dID in Lesson 01 for the Ball.xaml User Control,change Dot.xaml's LayoutRoot tag type from GrID to Canvas and remove the background color. If we also clear out the WIDth and Height property from the top User Control Tag,we will be left with a Silverlight User Control that looks and acts similar to an empty MovIEClip. Let's next add an Ellipse shape in Dot.xaml named ellipse with the same propertIEs as our original Flash movIEClip dot. So far,Dot.xaml should look like this:

At this point,you can right click on Dot.xaml in the Solution Explorer and choose to Open in Blend... if you want to visually create the XAML animation. I created a basic animation that would move the dot down while reducing its opacity,but any Storyboard you wish to create that will move the dot's location and fade it to zero opacity will do. Here is what my finished Blend screen looks like:

As you create your animation in Blend,the Storyboard deFinition is created in the XAML page as shown below. A couple things to point out. My Storyboard is named sbDrop,so that is how I will address it from code. Also,this storyboard has only been defined. It has not been given any command as to when it should execute. So to solve that,let's add a Loaded event handler in the Dot.xaml User Control as we dID prevIoUsly. And just as our Flash MovIEClip had a line of code to run when it reached the end of its animation (the code that would remove the movIEClip from the screen to clear up memory),we can add a Completed event handler to our time based sbDrop storyboard in Silverlight. Don't forget to hit Tab while scripting so Visual Studio can create the stub code for you in the Code Behind page.

Our Dot.xaml file should look something to this:

Press F7 to go to the code behind file Dot.xaml.cs and let's add some code to our User Control Loaded and Storyboard Completed event handlers. We want sbDrop to start playing as soon as the control loads,so we add this line of code here:

But how do we remove the User Control once its animation is completed? We don't want to leave every User Control we've added on the screen once they've played out their animation or we will start noticing the same memory drain we would notice in Flash. However,the equivalent one line statement may not be as obvIoUs in C#. The reason for this is that many Traditional developers may not consIDer it proper Coding practice to let children (the User Controls or MovIEClips) remove themselves directly as we experIEnced developers have grown accustom to in Flash. But rather,the reasoning goes,the dependent User Control or movIEClip should raise an event to their parent container (the LayoutRoot canvas in Page.xaml in our example) and have the parent manage the disposal. We will let others debate the pros and cons of these different approaches,but if you want to kNow how to duplicate removeMovIEClip(this) in Silverlight from within Dot.xaml.cs directly when the sbDrop Storyboard is completed,you can do it this way:

This only works when we are correct in assuming that the Parent of this instance of Dot is a Canvas. Since we are adding the dots in Page.xaml.cs to the LayoutRoot Canvas as visual children,this one line of code works great. There are no dependencIEs or business logic to these graphics. They exist simply to carry out their basic animation and disappear. Therefore,many of us coming from Flash should feel comfortable with this approach. However,for more complex animations where multiple dependencIEs between children may exist or where the visuals are coupled to data that may be lost or orphaned,I would suggest a more canonical C# eventing solution.

All we need to do Now is add the mouse move handler on Page.xaml. If you recall,we should have already added the Loaded event in the top User Control tag of Page.xaml so our code behind file should have the stub ready for us. The logic is almost IDentical to what we covered in Lesson01 as you can see here:

Press F5 to run it and you should see it work like so.

Improving the Code

Although we have duplicated the experIEnce in Silverlight,the lack of ability to type _x or _y against our User Controls can start feeling very cumbersome. Here is a work around that anyone coming from Flash will absolutely love.

Go back into your Dot.xaml.cs file and add the following public variables to your class deFinition like so:

You will see this X and Y deFinition added to every User Control we create where we need to position them through code. You should become very familiar with them over these 10 lessons!

This defines an X and Y property for your Dot.xaml User Control that sets the Canvas.left and Canvas.top dependency propertIEs indirectly in the getters and setters. So you can Now update your positioning code in Page.xaml.cs to the more familiar:

Finally,here is an updated Silverlight sample that uses the same logical code,but has a richer animation and design in the XAML. It is included in the download files below for your comparison.

Now that we've seen how Traditional Storyboard's work,In our next lesson we're going to see how we can start duplicating the onEnterFrame() functions that play such a big part of custom animations in Flash.

 

http://projectrosetta.com/ftsl/xamlanimations.aspx

总结

以上是内存溢出为你收集整理的使用Silverlight实现 FLASH的动画效果全部内容,希望文章能够帮你解决使用Silverlight实现 FLASH的动画效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1053796.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存