Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Ads by Lake Quincy Media
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Silverlight » Your first Animation using XAML and Silverlight - Point Animation class: Part III

Your first Animation using XAML and Silverlight - Point Animation class: Part III

This article is third part of the series of articles about animation using XAML and Silverlight.

Author Rank:
Total page views :  2453
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor


I the previous articles Your first animations using XAML and Silverlight - Color animation: Part I and Your first animations using XAML and Silverlight- Double animation: Part II, we discovered how to deal with ColorAnimation and DoubleAnimation classes. In this one we will show how to deal with PointAnimation class to perform a linear interpolation movement between two points of a given object. As a first step, we will perform this with xaml then we will do the same animation using the C# code.

If you have already developed Adobe Flash projects like me, you will observe that this kind of animation class helps to perform the same task as a given linear interpolation movement done with flash. 

Ok, let's consider an ellipse that moves from a point to another as mentioned within this bellow presentation:

Figure 1

To perform this, I propose this xaml and C# code:

XAML Code:

<Window x:Class="myWpfApplication.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="400" Width="400" Loaded="Window_Loaded">

    <!--I used canvas this once as container to host child element -->

        <Canvas>

            <!--Don't confuse with System.IO.Path class

            this one is System.Windows.Shapes.Path witch

            is responsible to draw curves-->

            <Path Fill="Beige" Margin="0,0,200,200">

                <Path.Data>

 

                    <!-- Describes an ellipse. -->

                    <EllipseGeometry x:Name="MyAnimatedEllipseGeometry"

            Center="200,110" RadiusX="150" RadiusY="100" />

                </Path.Data>

                <Path.Triggers>

                    <!--The Loaded event of the path class will

                    trigger the point aniamtion-->

                    <EventTrigger RoutedEvent="Path.Loaded">

                        <BeginStoryboard Name="MyBeginStoryboard">

                            <Storyboard>

 

                <!-- Animate the Center property so that the ellipse animates from

                one point on the screen to another. -->

                                <PointAnimation

                Storyboard.TargetProperty="Center"

                Storyboard.TargetName="MyAnimatedEllipseGeometry"

                Duration="0:0:8" From="200,110" To="200,290" RepeatBehavior="Forever" />

                            </Storyboard>

                        </BeginStoryboard>

                    </EventTrigger>

                </Path.Triggers>

            </Path>

        </Canvas>  

</Window>

In the xaml code editor copy and paste the above code instead of the existing code, then run the project and observe.

C# code:

To perform this task using C#, open a new WPF application and then add this code to the C# code editor, for instance, don't forget to include the both namespaces System.Windows.Media.Animation and System.Windows.Shapes if they aren't included yet.

//The shape container

        Canvas myCanvas;

        //The curve

        Path myPath;

        //The shape form

        EllipseGeometry myEllipseGeometry;

        //The PointAnimation instance

        PointAnimation oPointAnimation;

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            //Instantiate the canvas container

            myCanvas = new Canvas();

            //Instantiate the curve

            myPath = new Path();

            //The the fill color as beige

            myPath.Fill = Brushes.Beige;

            //Set the shape volume

            myPath.Margin = new Thickness(0, 0, 200, 200);

            /* instantiate the ellipse shape, you can make use of the ellipse

             * class directly instead of using ellipse geometry and path classes

             to draw an ellipse*/

            myEllipseGeometry = new EllipseGeometry(new Point(200, 110), 150, 100);

            //Set the path data to the given ellipse geometry

            myPath.Data = myEllipseGeometry;

            //Add the path to the canvas container

            myCanvas.Children.Add(myPath);

            //Finally, add the container to the form

            this.AddChild(myCanvas);

            myPath.Loaded+=new RoutedEventHandler(myPath_Loaded);

        }

        private void myPath_Loaded(object sender, RoutedEventArgs e)

        {

            oPointAnimation = new PointAnimation();

            oPointAnimation.From = new Point(200, 110);

            oPointAnimation.To = new Point(200, 290);

            oPointAnimation.RepeatBehavior = RepeatBehavior.Forever;

            oPointAnimation.Duration = TimeSpan.FromSeconds(8);

            myEllipseGeometry.BeginAnimation(EllipseGeometry.CenterProperty,oPointAnimation);

        }

Now run the project and you will remark the same behaviour as the first. You can combine animations, I mean double animation, color animation and point animation to build a complex one. 

Good Dotneting!!!


Login to add your contents and source code to this article
 About the author
 
Bechir Bejaoui
The author holds a master degree in NTIC specialized  in software developement delivered by the high school of communication SUPCOM, he also holds a bachelor degree in finance delivered by  the  economic sciences and  management  university of Tunis "FSEGT". He's a freelance developer since 2006. Actually woking on the WPF, .Net framewok 3.5, silverlight and the other .Net new features, in addition, he is painter and sculptor.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
Exception by shishir On February 10, 2009
Hey Bechir, I Tried ur code.... but there is some exception in this line "this.AddChild(myCanvas);" <-------- It is InvalidOperationException <- "Content of a ContentControl must be a single element." I tried this instead "AddLogicalChild(myCanvas)" & "AddVisualChild(myCanvas)". This works .... but the problem is .... I'm not getting for what purpose u added the PointAnimation Object.... Because changing in Points... Never gets reflected in the animation. Am I doing something wrong? Plz Help.!
Reply | Email | Delete | Modify | 
Re: Exception by Bechir On February 11, 2009

Hi,
Ok my freind, I will test the code again, I try to find out the problem and respond you as soon as possible
Reply | Email | Delete | Modify | 
Re: Exception by Bechir On February 11, 2009
Hi again shishir,
I tested the C# code and it works fine and in order to be convinced I recorded the test in a video and uploaded it so you can download this link and see the test.

http://rapidshare.com/files/196751420/shishir.zip.html?killcode=9114431685969873463

But nevertheless, I will try to help you step over this difficulty by sending exactly what you have used as code so that I can test it by my self

About the second issue, I mean why did I add a point animation to my code since it doesn't reflect the animation it self? (If I well understood the question)

I tried in a second video to show you, if I change the point aniamtion proprties to this
private void myPath_Loaded(object sender, RoutedEventArgs e)
        {

            oPointAnimation = new PointAnimation();
            oPointAnimation.From = new Point(100, 100);
            oPointAnimation.To = new Point(300, 100);
            oPointAnimation.RepeatBehavior = RepeatBehavior.Forever;
            oPointAnimation.Duration = TimeSpan.FromSeconds(2);
            myEllipseGeometry.BeginAnimation(EllipseGeometry.CenterProperty, oPointAnimation);

        }
, the animation will entirely change the direction and the duration.
I've povided  a second video to demonstrate that

http://rapidshare.com/files/196757752/shishir2.zip.html?killcode=6994300746941738860

Ok let's make a sort of resume to well understand the issue

The PointAnimation class creates a transition between two target values. To set its target values,it makes use of its From, To, and By properties using linear interpolation over a specified Duration, the point animation is used in two senarios either to move a shape from a point to another point or to change the size of the given shape. For more info about point animation I invite you to consult this helpful MSDN link, me personally I've learned the animation point from this link.

http://msdn.microsoft.com/en-us/library/system.windows.media.animation.pointanimation.aspx

May you have a little bit confused with Double aniamtion, what can gives a double aniamtion over a point aniamtion is that it moves the given shape of the first case by changing its color at the same time. This is the crucial difference between both point animation and double animation.

http://msdn.microsoft.com/en-us/library/system.windows.media.animation.doubleanimation.aspx

Finally, I hope that I've answered your question, if no, Of Corse, I'm hier to clarify the ambigutiy? so please feel free to ask your questions
Thanks

Reply | Email | Delete | Modify | 
Re: Re: Exception by shishir On February 11, 2009

Hi Bechir,

 

I'm  sorry, because I've made a mistake probably because of lack of knowledge.

The code is working absolutely fine...

The problem was, I was copying both XAML code and CS code in one program.

but they are actually two solution of the same thing.

 

Thanks for solving my problem. I really appreciate your response.

Reply | Email | Delete | Modify | 
Re: Re: Re: Exception by Bechir On February 11, 2009

Hi Shishir,
Don't worry my freind, every body may make mistakes,
 you're welcome.
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.