Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | 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
 Resources  
Close
 Our Network  
Close
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:
Total page views :  8269
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor

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


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
Double Animation by Frank On February 9, 2009
Did You use WPF or Silverlight to get this example to Work?
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.