|
Este artículo proviene de un motor de traducción automática. Mueva el puntero sobre las frases del artículo para ver el texto original. Más información.
|
Traducción
Original
|
Información general sobre objetos Storyboard
-
Aumentar de tamaño y cambiar de color cuando el usuario selecciona el botón. -
Disminuir de tamaño y volver a su tamaño original cuando se hace clic en él. -
Disminuir de tamaño y desvanecerse con un fundido al 50% de opacidad al deshabilitarse.
-
Animar un SolidColorBrush (elemento que no es de marco) que pinta el fondo de un botón (un tipo de FrameworkElement) -
Animar un SolidColorBrush (elemento que no es de marco) que pinta el relleno de un GeometryDrawing (elemento que no es de marco) mostrado mediante una Image (FrameworkElement). -
Mediante código, animar un SolidColorBrush declarado por una clase que también contiene un FrameworkElement, si SolidColorBrush ha registrado su nombre con FrameworkElement.
|
|
|
|
|
|
|
|---|---|---|---|---|---|
|
|
|
|
|
||
|
|
|
|
|
|
Cómo: Activar una animación al cambiar el valor de una propiedad |
|
|
|
|
|
||
|
|
|
|
|
|
<!-- This example shows how to animate with a storyboard.--> <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="Microsoft.Samples.Animation.StoryboardsExample" WindowTitle="Storyboards Example"> <StackPanel Margin="20"> <Rectangle Name="MyRectangle" Width="100" Height="100"> <Rectangle.Fill> <SolidColorBrush x:Name="MySolidColorBrush" Color="Blue" /> </Rectangle.Fill> <Rectangle.Triggers> <EventTrigger RoutedEvent="Rectangle.MouseEnter"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="MyRectangle" Storyboard.TargetProperty="Width" From="100" To="200" Duration="0:0:1" /> <ColorAnimation Storyboard.TargetName="MySolidColorBrush" Storyboard.TargetProperty="Color" From="Blue" To="Red" Duration="0:0:1" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle> </StackPanel> </Page>
Imports System Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Media Imports System.Windows.Media.Animation Imports System.Windows.Data Imports System.Windows.Shapes Imports System.Windows.Input Namespace Microsoft.Samples.Animation Public Class StoryboardsExample Inherits Page Public Sub New() Me.WindowTitle = "Storyboards Example" Dim myStackPanel As New StackPanel() myStackPanel.Margin = New Thickness(20) Dim myRectangle As New Rectangle() myRectangle.Name = "MyRectangle" ' Create a name scope for the page. NameScope.SetNameScope(Me, New NameScope()) Me.RegisterName(myRectangle.Name, myRectangle) myRectangle.Width = 100 myRectangle.Height = 100 Dim mySolidColorBrush As New SolidColorBrush(Colors.Blue) Me.RegisterName("MySolidColorBrush", mySolidColorBrush) myRectangle.Fill = mySolidColorBrush Dim myDoubleAnimation As New DoubleAnimation() myDoubleAnimation.From = 100 myDoubleAnimation.To = 200 myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(1)) Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name) Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty)) Dim myColorAnimation As New ColorAnimation() myColorAnimation.From = Colors.Blue myColorAnimation.To = Colors.Red myColorAnimation.Duration = New Duration(TimeSpan.FromSeconds(1)) Storyboard.SetTargetName(myColorAnimation, "MySolidColorBrush") Storyboard.SetTargetProperty(myColorAnimation, New PropertyPath(SolidColorBrush.ColorProperty)) Dim myStoryboard As New Storyboard() myStoryboard.Children.Add(myDoubleAnimation) myStoryboard.Children.Add(myColorAnimation) AddHandler myRectangle.MouseEnter, Sub(sender As Object, e As MouseEventArgs) myStoryboard.Begin(Me) myStackPanel.Children.Add(myRectangle) Me.Content = myStackPanel End Sub End Class End Namespace
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Data; using System.Windows.Shapes; using System.Windows.Input; namespace Microsoft.Samples.Animation { public class StoryboardsExample : Page { public StoryboardsExample() { this.WindowTitle = "Storyboards Example"; StackPanel myStackPanel = new StackPanel(); myStackPanel.Margin = new Thickness(20); Rectangle myRectangle = new Rectangle(); myRectangle.Name = "MyRectangle"; // Create a name scope for the page. NameScope.SetNameScope(this, new NameScope()); this.RegisterName(myRectangle.Name, myRectangle); myRectangle.Width = 100; myRectangle.Height = 100; SolidColorBrush mySolidColorBrush = new SolidColorBrush(Colors.Blue); this.RegisterName("MySolidColorBrush", mySolidColorBrush); myRectangle.Fill = mySolidColorBrush; DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = 100; myDoubleAnimation.To = 200; myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name); Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty)); ColorAnimation myColorAnimation = new ColorAnimation(); myColorAnimation.From = Colors.Blue; myColorAnimation.To = Colors.Red; myColorAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); Storyboard.SetTargetName(myColorAnimation, "MySolidColorBrush"); Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty)); Storyboard myStoryboard = new Storyboard(); myStoryboard.Children.Add(myDoubleAnimation); myStoryboard.Children.Add(myColorAnimation); myRectangle.MouseEnter += delegate(object sender, MouseEventArgs e) { myStoryboard.Begin(this); }; myStackPanel.Children.Add(myRectangle); this.Content = myStackPanel; } } }
<Rectangle Name="MyRectangle" Width="100" Height="100">
Dim myRectangle As New Rectangle() myRectangle.Name = "MyRectangle" ' Create a name scope for the page. NameScope.SetNameScope(Me, New NameScope()) Me.RegisterName(myRectangle.Name, myRectangle)
Rectangle myRectangle = new Rectangle(); myRectangle.Name = "MyRectangle"; // Create a name scope for the page. NameScope.SetNameScope(this, new NameScope()); this.RegisterName(myRectangle.Name, myRectangle);
<DoubleAnimation Storyboard.TargetName="MyRectangle" Storyboard.TargetProperty="Width" From="100" To="200" Duration="0:0:1" />
Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name)
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty))
Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
Storyboard.SetTargetProperty(myDoubleAnimation,
new PropertyPath(Rectangle.WidthProperty));
<SolidColorBrush x:Name="MySolidColorBrush" Color="Blue" />
<ColorAnimation Storyboard.TargetName="MySolidColorBrush" Storyboard.TargetProperty="Color" From="Blue" To="Red" Duration="0:0:1" />
Storyboard.SetTargetName(myColorAnimation, "MySolidColorBrush") Storyboard.SetTargetProperty(myColorAnimation, New PropertyPath(SolidColorBrush.ColorProperty))
Storyboard.SetTargetName(myColorAnimation, "MySolidColorBrush"); Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty));
<ColorAnimation Storyboard.TargetName="Rectangle01" Storyboard.TargetProperty="Fill.Color" From="Blue" To="AliceBlue" Duration="0:0:1" />
Dim propertyChain() As DependencyProperty = {Rectangle.FillProperty, SolidColorBrush.ColorProperty} Dim thePath As String = "(0).(1)" Dim myPropertyPath As New PropertyPath(thePath, propertyChain) Storyboard.SetTargetProperty(myColorAnimation, myPropertyPath)
DependencyProperty[] propertyChain =
new DependencyProperty[]
{Rectangle.FillProperty, SolidColorBrush.ColorProperty};
string thePath = "(0).(1)";
PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain);
Storyboard.SetTargetProperty(myColorAnimation, myPropertyPath);
Establecer de manera indirecta una propiedad de destino de un objeto inmovilizable en XAML
|
|
-
ElementPropertyName es la propiedad de FrameworkElement que se establece mediante Freezable y -
FreezablePropertyName es la propiedad de Freezable que se va a animar.
<Rectangle Name="Rectangle01" Height="100" Width="100" Fill="{StaticResource MySolidColorBrushResource}"> <Rectangle.Triggers> <EventTrigger RoutedEvent="Rectangle.MouseEnter"> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="Rectangle01" Storyboard.TargetProperty="Fill.Color" From="Blue" To="AliceBlue" Duration="0:0:1" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle>
|
|
<TransformGroup x:Key="MyTransformGroupResource" x:Shared="False"> <ScaleTransform /> <RotateTransform /> </TransformGroup>
<Rectangle Name="Rectangle02" Height="100" Width="100" Fill="Blue" RenderTransform="{StaticResource MyTransformGroupResource}"> <Rectangle.Triggers> <EventTrigger RoutedEvent="Rectangle.MouseEnter"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="Rectangle02" Storyboard.TargetProperty="RenderTransform.Children[1].Angle" From="0" To="360" Duration="0:0:1" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle>
Establecer de manera indirecta una propiedad de destino de un objeto inmovilizable mediante código
|
|
-
OwnerPropertyArrayIndex es el índice de la matriz de DependencyProperty que contiene el identificador de la propiedad del objeto FrameworkElement que se establece mediante Freezable y -
FreezablePropertyArrayIndex es el índice de la matriz de DependencyProperty que contiene el identificador de la propiedad que se establece como destino.
// Create a name scope for the page. NameScope.SetNameScope(this, new NameScope()); Rectangle rectangle01 = new Rectangle(); rectangle01.Name = "Rectangle01"; this.RegisterName(rectangle01.Name, rectangle01); rectangle01.Width = 100; rectangle01.Height = 100; rectangle01.Fill = (SolidColorBrush)this.Resources["MySolidColorBrushResource"]; ColorAnimation myColorAnimation = new ColorAnimation(); myColorAnimation.From = Colors.Blue; myColorAnimation.To = Colors.AliceBlue; myColorAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); Storyboard.SetTargetName(myColorAnimation, rectangle01.Name); DependencyProperty[] propertyChain = new DependencyProperty[] {Rectangle.FillProperty, SolidColorBrush.ColorProperty}; string thePath = "(0).(1)"; PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain); Storyboard.SetTargetProperty(myColorAnimation, myPropertyPath); Storyboard myStoryboard = new Storyboard(); myStoryboard.Children.Add(myColorAnimation); BeginStoryboard myBeginStoryboard = new BeginStoryboard(); myBeginStoryboard.Storyboard = myStoryboard; EventTrigger myMouseEnterTrigger = new EventTrigger(); myMouseEnterTrigger.RoutedEvent = Rectangle.MouseEnterEvent; myMouseEnterTrigger.Actions.Add(myBeginStoryboard); rectangle01.Triggers.Add(myMouseEnterTrigger);
<TransformGroup x:Key="MyTransformGroupResource" x:Shared="False"> <ScaleTransform /> <RotateTransform /> </TransformGroup>
|
|
DependencyProperty[] propertyChain =
new DependencyProperty[]
{
Rectangle.RenderTransformProperty,
TransformGroup.ChildrenProperty,
RotateTransform.AngleProperty
};
string thePath = "(0).(1)[1].(2)";
PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain);
Storyboard.SetTargetProperty(myDoubleAnimation, myPropertyPath);
Rectangle rectangle02 = new Rectangle(); rectangle02.Name = "Rectangle02"; this.RegisterName(rectangle02.Name, rectangle02); rectangle02.Width = 100; rectangle02.Height = 100; rectangle02.Fill = Brushes.Blue; rectangle02.RenderTransform = (TransformGroup)this.Resources["MyTransformGroupResource"]; DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = 0; myDoubleAnimation.To = 360; myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); Storyboard.SetTargetName(myDoubleAnimation, rectangle02.Name); DependencyProperty[] propertyChain = new DependencyProperty[] { Rectangle.RenderTransformProperty, TransformGroup.ChildrenProperty, RotateTransform.AngleProperty }; string thePath = "(0).(1)[1].(2)"; PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain); Storyboard.SetTargetProperty(myDoubleAnimation, myPropertyPath); Storyboard myStoryboard = new Storyboard(); myStoryboard.Children.Add(myDoubleAnimation); BeginStoryboard myBeginStoryboard = new BeginStoryboard(); myBeginStoryboard.Storyboard = myStoryboard; EventTrigger myMouseEnterTrigger = new EventTrigger(); myMouseEnterTrigger.RoutedEvent = Rectangle.MouseEnterEvent; myMouseEnterTrigger.Actions.Add(myBeginStoryboard); rectangle02.Triggers.Add(myMouseEnterTrigger);
Establecimiento indirecto de destinos con un objeto inmovilizable como punto de partida
-
PauseStoryboard : pone en pausa el guión gráfico. -
ResumeStoryboard : reanuda un guión gráfico que se ha puesto en pausa. -
SetStoryboardSpeedRatio : cambia la velocidad del guión gráfico. -
SkipStoryboardToFill : avanza un guión gráfico hasta el final de su período de relleno, si lo tiene. -
StopStoryboard : detiene el guión gráfico. -
RemoveStoryboard : quita el guión gráfico.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="Microsoft.SDK.Animation.ControllableStoryboardExample" WindowTitle="Fading Rectangle Example"> <StackPanel Margin="10"> <Rectangle Name="MyRectangle" Width="100" Height="100" Fill="Blue"> </Rectangle> <Button Name="BeginButton">Begin</Button> <Button Name="PauseButton">Pause</Button> <Button Name="ResumeButton">Resume</Button> <Button Name="SkipToFillButton">Skip To Fill</Button> <Button Name="StopButton">Stop</Button> <StackPanel.Triggers> <EventTrigger RoutedEvent="Button.Click" SourceName="BeginButton"> <BeginStoryboard Name="MyBeginStoryboard"> <Storyboard> <DoubleAnimation Storyboard.TargetName="MyRectangle" Storyboard.TargetProperty="(Rectangle.Opacity)" From="1.0" To="0.0" Duration="0:0:5" /> </Storyboard> </BeginStoryboard> </EventTrigger> <EventTrigger RoutedEvent="Button.Click" SourceName="PauseButton"> <PauseStoryboard BeginStoryboardName="MyBeginStoryboard" /> </EventTrigger> <EventTrigger RoutedEvent="Button.Click" SourceName="ResumeButton"> <ResumeStoryboard BeginStoryboardName="MyBeginStoryboard" /> </EventTrigger> <EventTrigger RoutedEvent="Button.Click" SourceName="SkipToFillButton"> <SkipStoryboardToFill BeginStoryboardName="MyBeginStoryboard" /> </EventTrigger> <EventTrigger RoutedEvent="Button.Click" SourceName="StopButton"> <StopStoryboard BeginStoryboardName="MyBeginStoryboard" /> </EventTrigger> </StackPanel.Triggers> </StackPanel> </Page>
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); } } }
-
No se especifica un TargetName; Storyboard siempre establece como destino el elemento al que se aplica el Style. Para establecer como destino objetos Freezable, debe utilizar el establecimiento indirecto de destinos. Para obtener más información sobre el establecimiento indirecto de destinos, consulte la sección Establecimiento indirecto de destinos. -
No se puede especificar un SourceName para un EventTrigger o un Trigger. -
No se pueden utilizar referencias de recursos dinámicos ni expresiones de enlace de datos para establecer Storyboard o valores de propiedades de animación. Esto se debe a que todo lo que esté dentro de un Style debe ser seguro para subprocesos y el sistema de control de tiempo debe inmovilizar (Freeze) los objetos Storyboard a fin de hacerlos seguros para subprocesos. No se puede inmovilizar un Storyboard si éste o sus escalas de tiempo secundarias contienen referencias de recursos dinámicos o expresiones de enlace de datos. Para obtener más información sobre la inmovilización y otras características de Freezable, consulte Información general sobre objetos Freezable. -
En XAML, no se pueden declarar controladores de eventos para Storyboard ni eventos de animación.
-
TargetName sólo puede hacer referencia a los objetos secundarios de ControlTemplate. Si no se especifica TargetName, la animación establece como destino el elemento al que se aplica ControlTemplate. -
El SourceName de un EventTrigger o de un Trigger únicamente puede hacer referencia a objetos secundarios de ControlTemplate. -
No se pueden utilizar referencias de recursos dinámicos ni expresiones de enlace de datos para establecer Storyboard o valores de propiedades de animación. Esto se debe a que todo lo que esté dentro de un ControlTemplate debe ser seguro para subprocesos y el sistema de control de tiempo debe inmovilizar (Freeze) los objetos Storyboard a fin de hacerlos seguros para subprocesos. No se puede inmovilizar un Storyboard si éste o sus escalas de tiempo secundarias contienen referencias de recursos dinámicos o expresiones de enlace de datos. Para obtener más información sobre la inmovilización y otras características de Freezable, consulte Información general sobre objetos Freezable. -
En XAML, no se pueden declarar controladores de eventos para Storyboard ni eventos de animación.