Export (0) Print
Expand All

Storyboard.Begin Method (FrameworkElement, HandoffBehavior, Boolean)

Applies the animations associated with this Storyboard to their targets and initiates them.

Namespace:  System.Windows.Media.Animation
Assembly:  PresentationFramework (in PresentationFramework.dll)

public void Begin(
	FrameworkElement containingObject,
	HandoffBehavior handoffBehavior,
	bool isControllable
)

Parameters

containingObject
Type: System.Windows.FrameworkElement

An object contained within the same name scope as the targets of this storyboard's animations. Animations without a specified TargetName are applied to containingObject.

handoffBehavior
Type: System.Windows.Media.Animation.HandoffBehavior

The behavior the new animation should use to interact with any current animations.

isControllable
Type: System.Boolean

Declares whether the animation is controllable (can be paused) once started.

To interactively control this storyboard, you must use the same containingObject parameter when calling the interactive methods that you used to begin the storyboard

When this method is called, Clock objects are created for the storyboard and any timelines it contains. These clocks are stored with containingObject.

Beginning a storyboard triggers the CurrentStateInvalidated and CurrentGlobalSpeedInvalidated events.

Using the Compose HandoffBehavior

When you apply a Storyboard, AnimationTimeline, or AnimationClock to a property using the Compose HandoffBehavior, any Clock objects previously associated with that property continue to consume system resources; the timing system does not remove these clocks automatically.

To avoid performance issues when you apply a large number of clocks using Compose, you should remove composing clocks from the animated property after they complete. There are several ways to remove a clock.

This is primarily an issue for animations on objects that have a long lifetime. When an object is garbage collected, its clocks are also disconnected and garbage collected.

For more information about clock objects, see Animation and Timing System Overview.

The following example shows how to create a controllable storyboard.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace SDKSample
{

    public class ControllableStoryboardExample : Page
    {
        private Storyboard myStoryboard;

        public ControllableStoryboardExample()
        {

            // Create a name scope for the page.

            NameScope.SetNameScope(this, new NameScope()); 

            this.WindowTitle = "Controllable Storyboard Example";
            StackPanel myStackPanel = new StackPanel();
            myStackPanel.Margin = new Thickness(10);

            // Create a rectangle.
            Rectangle myRectangle = new Rectangle();
            myRectangle.Name = "myRectangle";

            // Assign the rectangle a name by  
            // registering it with the page, so that 
            // it can be targeted by storyboard 
            // animations. 
            this.RegisterName(myRectangle.Name, myRectangle);
            myRectangle.Width = 100;
            myRectangle.Height = 100;
            myRectangle.Fill = Brushes.Blue;
            myStackPanel.Children.Add(myRectangle);

            // 
            // Create an animation and a storyboard to animate the 
            // rectangle. 
            //
            DoubleAnimation myDoubleAnimation = new DoubleAnimation();
            myDoubleAnimation.From = 1.0;
            myDoubleAnimation.To = 0.0;
            myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(5000));
            myDoubleAnimation.AutoReverse = true;

            // Create the storyboard.
            myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation);
            Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.OpacityProperty));

            // 
            // Create some buttons to control the storyboard 
            // and a panel to contain them. 
            //
            StackPanel buttonPanel = new StackPanel();
            buttonPanel.Orientation = Orientation.Horizontal;
            Button beginButton = new Button();
            beginButton.Content = "Begin";
            beginButton.Click += new RoutedEventHandler(beginButton_Clicked);
            buttonPanel.Children.Add(beginButton);
            Button pauseButton = new Button();
            pauseButton.Content = "Pause";
            pauseButton.Click += new RoutedEventHandler(pauseButton_Clicked);
            buttonPanel.Children.Add(pauseButton);
            Button resumeButton = new Button();
            resumeButton.Content = "Resume";
            resumeButton.Click += new RoutedEventHandler(resumeButton_Clicked);
            buttonPanel.Children.Add(resumeButton);
            Button skipToFillButton = new Button();
            skipToFillButton.Content = "Skip to Fill";
            skipToFillButton.Click += new RoutedEventHandler(skipToFillButton_Clicked);
            buttonPanel.Children.Add(skipToFillButton);
            Button setSpeedRatioButton = new Button();
            setSpeedRatioButton.Content = "Triple Speed";
            setSpeedRatioButton.Click += new RoutedEventHandler(setSpeedRatioButton_Clicked);
            buttonPanel.Children.Add(setSpeedRatioButton);
            Button stopButton = new Button();
            stopButton.Content = "Stop";
            stopButton.Click += new RoutedEventHandler(stopButton_Clicked);
            buttonPanel.Children.Add(stopButton);
            myStackPanel.Children.Add(buttonPanel);
            this.Content = myStackPanel;        


        }

        // Begins the storyboard. 
        private void beginButton_Clicked(object sender, RoutedEventArgs args)
        {
            // Specifying "true" as the second Begin parameter
            // makes this storyboard controllable.
            myStoryboard.Begin(this, true);

        }

        // Pauses the storyboard. 
        private void pauseButton_Clicked(object sender, RoutedEventArgs args)
        {
            myStoryboard.Pause(this);

        }

        // Resumes the storyboard. 
        private void resumeButton_Clicked(object sender, RoutedEventArgs args)
        {
            myStoryboard.Resume(this);

        }

        // Advances the storyboard to its fill period. 
        private void skipToFillButton_Clicked(object sender, RoutedEventArgs args)
        {
            myStoryboard.SkipToFill(this);

        }

        // Updates the storyboard's speed. 
        private void setSpeedRatioButton_Clicked(object sender, RoutedEventArgs args)
        {
            // Makes the storyboard progress three times as fast as normal.
            myStoryboard.SetSpeedRatio(this, 3);

        }

        // Stops the storyboard. 
        private void stopButton_Clicked(object sender, RoutedEventArgs args)
        {
            myStoryboard.Stop(this);

        }         

    }

}

The next example uses the SnapshotAndReplace HandoffBehavior to animate when the user left-clicks, and the Compose HandoffBehavior when the user right-clicks.

/*

   This sample animates the position of an ellipse when 
   the user clicks within the main border. If the user
   left-clicks, the SnapshotAndReplace HandoffBehavior
   is used when applying the animations. If the user
   right-clicks, the Compose HandoffBehavior is used
   instead.

*/ 

using System;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Input;

namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
{

    // Create the demonstration. 
    public class FrameworkElementStoryboardHandoffBehaviorExample : Page {


        private Border containerBorder;
        private Ellipse interactiveEllipse;
        private Storyboard theStoryboard;
        private DoubleAnimation xAnimation;
        private DoubleAnimation yAnimation;

        public FrameworkElementStoryboardHandoffBehaviorExample()
        {

            WindowTitle = "Interactive Animation Example";

            // Create a name scope for the page.
            NameScope.SetNameScope(this, new NameScope());  

            DockPanel myPanel = new DockPanel();
            myPanel.Margin = new Thickness(20.0);            

            containerBorder = new Border();
            containerBorder.Background = Brushes.White;
            containerBorder.BorderBrush = Brushes.Black;
            containerBorder.BorderThickness = new Thickness(2.0); 
            containerBorder.VerticalAlignment = VerticalAlignment.Stretch;

            interactiveEllipse = new Ellipse();
            interactiveEllipse.Fill = Brushes.Lime;
            interactiveEllipse.Stroke = Brushes.Black;
            interactiveEllipse.StrokeThickness = 2.0;
            interactiveEllipse.Width = 25;
            interactiveEllipse.Height = 25;
            interactiveEllipse.HorizontalAlignment = HorizontalAlignment.Left;
            interactiveEllipse.VerticalAlignment = VerticalAlignment.Top;


            TranslateTransform interactiveTranslateTransform = new TranslateTransform();       
            this.RegisterName("InteractiveTranslateTransform", interactiveTranslateTransform);

            interactiveEllipse.RenderTransform = 
                interactiveTranslateTransform;

            xAnimation = new DoubleAnimation();
            xAnimation.Duration = TimeSpan.FromSeconds(4);
            yAnimation = xAnimation.Clone();
            Storyboard.SetTargetName(xAnimation, "InteractiveTranslateTransform");
            Storyboard.SetTargetProperty(xAnimation, new PropertyPath(TranslateTransform.XProperty));
            Storyboard.SetTargetName(yAnimation, "InteractiveTranslateTransform");
            Storyboard.SetTargetProperty(yAnimation, new PropertyPath(TranslateTransform.YProperty));            

            theStoryboard = new Storyboard();
            theStoryboard.Children.Add(xAnimation);
            theStoryboard.Children.Add(yAnimation);


            containerBorder.MouseLeftButtonDown += 
                new MouseButtonEventHandler(border_mouseLeftButtonDown);
            containerBorder.MouseRightButtonDown += 
                new MouseButtonEventHandler(border_mouseRightButtonDown);                

            containerBorder.Child = interactiveEllipse;
            myPanel.Children.Add(containerBorder);
            this.Content = myPanel;
        }


        // When the user left-clicks, use the  
        // SnapshotAndReplace HandoffBehavior when applying the animation.         
        private void border_mouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            Point clickPoint = Mouse.GetPosition(containerBorder);

            // Set the target point so the center of the ellipse 
            // ends up at the clicked point.
            Point targetPoint = new Point();
            targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2;
            targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2;  

            // Animate to the target point.
            xAnimation.To = targetPoint.X;
            yAnimation.To = targetPoint.Y;
            theStoryboard.Begin(this, HandoffBehavior.SnapshotAndReplace);


            // Change the color of the ellipse.
            interactiveEllipse.Fill = Brushes.Lime;

        }

        // When the user right-clicks, use the  
        // Compose HandoffBehavior when applying the animation. 
        private void border_mouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {

            // Find the point where the use clicked.
            Point clickPoint = Mouse.GetPosition(containerBorder);

            // Set the target point so the center of the ellipse 
            // ends up at the clicked point.
            Point targetPoint = new Point();
            targetPoint.X = clickPoint.X - interactiveEllipse.Width / 2;
            targetPoint.Y = clickPoint.Y - interactiveEllipse.Height / 2;

            // Animate to the target point.
            xAnimation.To = targetPoint.X;
            yAnimation.To = targetPoint.Y;
            theStoryboard.Begin(this, HandoffBehavior.Compose);

            // Change the color of the ellipse.
            interactiveEllipse.Fill = Brushes.Orange;


        }

    }

}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft