Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » XAML » Your first animations using XAML and Silverlight- Double animation: Part II

Your first animations using XAML and Silverlight- Double animation: Part II

In the previous article "Your first animations using xaml and silverlight - Color animation: Part I", we've exposed a technique of how to deal with color animation. In this article, I will do same thing but with a different animation. I mean the DoubleAnimation class this time.

Author Rank :
Page Views : 16300
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

In the previous article "Your first animations using xaml and silverlight - Color animation: Part I", we've exposed a technique of how to deal with color animation. In this article, I will do same thing but with a different animation. I mean the DoubleAnimation class this time.

Remember if you have already deal with some Ajax frameworks, when under certain conditions you can obtain rounded corners div or panel. We will do the same thing through using a double animation to perform an interpolation between two states. I mean, we will transform the four sharp corners of a given rectangle to rounded corners when the mouse enters the rectangle and the inverse animation will be performed if the mouse leaves the rectangle. We do so using the both RadiusX and RadiusY properties.

If you run both XAML and C# pieces of code, you will observe this phenomenon:

Figure 1

Figure 2
 
To perform this I propose both codes:

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

<!—The rectangle extends the Animatable class so it can be target of an

 Animation therefore a chose it -->

 

    <Rectangle Name="myRectangle" Width="300" Height="300">

        <Rectangle.Triggers>

<!—The mouse enter event is the one that triggers the animation -->

            <EventTrigger RoutedEvent="Rectangle.MouseEnter">

                <BeginStoryboard>

<!—The Storyboard is a sort of aniamtion container-->

 

                    <Storyboard>

<!—The duration and the targeted properties witch are RadiusX, RadiusY, will Be subject of the aniamtions, all parameters are set within the animations tag  -->

 

                        <DoubleAnimation Storyboard.TargetName="myRectangle"

                                      Storyboard.TargetProperty="(Rectangle.RadiusX)"

                                         From="0" To="100"

                                         Duration="0:0:8"/>

                        <DoubleAnimation Storyboard.TargetName="myRectangle"

                                      Storyboard.TargetProperty="(Rectangle.RadiusY)"

                                         From="0" To="100"

                                         Duration="0:0:8"/>

                    </Storyboard>

                </BeginStoryboard>

            </EventTrigger>

            <EventTrigger RoutedEvent="Rectangle.MouseLeave">

                <BeginStoryboard>

                    <Storyboard>

                        <DoubleAnimation Storyboard.TargetName="myRectangle"

                                      Storyboard.TargetProperty="(Rectangle.RadiusX)"

                                         From="100" To="0"

                                         Duration="0:0:8"/>

                        <DoubleAnimation Storyboard.TargetName="myRectangle"

                                      Storyboard.TargetProperty="(Rectangle.RadiusY)"

                                         From="100" To="0"

                                         Duration="0:0:8"/>

                    </Storyboard>

                </BeginStoryboard>

            </EventTrigger>

        </Rectangle.Triggers>

    </Rectangle>

</Window>

C# code:

In order to perform the same task, open a new project>WPFApplication then drag and drop a new rectangle.

Figure 3

Then right click on it and choose the properties menu.

Figure 4

Then, select the properties menu item and set it property name to "myRectangle" width to "250" and it height to "250".

Then implement the code as below, but don't forget to append System.Windows.Media.Animation namespace to the project:

//This animation is for RadiusX

DoubleAnimation oDoubleAnimation1;

//This animation is for RadiusY

DoubleAnimation oDoubleAnimation2;

private void Window_Loaded(object sender, RoutedEventArgs e)

{

//Change the rectangle back color to beige

myRectangle.Fill = Brushes.Beige;

//Those two lines are responsibles for triggering events MouseEnter and MouseLeave

myRectangle.MouseEnter+=new MouseEventHandler(myRectangle_MouseEnter);

myRectangle.MouseLeave+=new MouseEventHandler(myRectangle_MouseLeave);

}

private void myRectangle_MouseEnter(object sender, RoutedEventArgs e)

{

//Set the oDoubleAnimation1

oDoubleAnimation1 = new DoubleAnimation();

//Set the transformation duration to 8 seconds

oDoubleAnimation1.Duration = TimeSpan.FromSeconds(8);

//The initial RadiusX value

oDoubleAnimation1.From = 0;

//The final RadiusX value

oDoubleAnimation1.To = 100;

//Trigger the animation

myRectangle.BeginAnimation(Rectangle.RadiusXProperty, oDoubleAnimation1);

//Set the oDoubleAnimation2

oDoubleAnimation2 = new DoubleAnimation();

//Set the transformation duration to 8 seconds

oDoubleAnimation2.Duration = TimeSpan.FromSeconds(8);

//The initial RadiusY value

oDoubleAnimation2.From = 0;

//The final RadiusY value

oDoubleAnimation2.To = 100;

//Trigger the animation

myRectangle.BeginAnimation(Rectangle.RadiusYProperty, oDoubleAnimation2);

 

}

private void myRectangle_MouseLeave(object sender, RoutedEventArgs e)

{

//Set the oDoubleAnimation1

oDoubleAnimation1 = new DoubleAnimation();

//Set the transformation duration to 8 seconds

oDoubleAnimation1.Duration = TimeSpan.FromSeconds(8);

//The initial RadiusX value

oDoubleAnimation1.From = 100;

//The final RadiusX value

oDoubleAnimation1.To = 0;

//Trigger the animation

myRectangle.BeginAnimation(Rectangle.RadiusXProperty, oDoubleAnimation1);

//Set the oDoubleAnimation2

oDoubleAnimation2 = new DoubleAnimation();

//Set the transformation duration to 8 seconds

oDoubleAnimation2.Duration = TimeSpan.FromSeconds(8);

//The initial RadiusY value

oDoubleAnimation2.From = 100;

//The final RadiusY value

oDoubleAnimation2.To = 0;

//Trigger the animation

myRectangle.BeginAnimation(Rectangle.RadiusYProperty, oDoubleAnimation2);

}

As you see the same task could be performed using either XAML or C# code behind.

Good Dotneting!!!

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate 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 also holds:

MCPD enteprise solutions developement 3.5 certification and MCTS distibuted application developement 2.0

 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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Double Animation by Frank On February 9, 2009
Did You use WPF or Silverlight to get this example to Work?
Reply | Email | Modify 
Double Animation by Marty On March 25, 2011
I think that there was something left out of the xaml part of the code.
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.