Freigeben über


Übersicht der Animationen von/nach/durch

In diesem Thema wird beschrieben, wie Sie From/To/By-Animationen verwenden, um Abhängigkeitseigenschaften zu animieren. Eine From/To/By-Animation erstellt einen Übergang zwischen zwei Werten.

Voraussetzungen

Um dieses Thema zu verstehen, sollten Sie mit WPF-Animationsfeatures vertraut sein. Eine Einführung in Animationsfeatures finden Sie in der Animationsübersicht.

Was ist eine "From/To/By"-Animation?

Eine From/To/By-Animation ist ein Animationstyp, der AnimationTimeline einen Übergang zwischen einem Anfangswert und einem Endwert schafft. Die Dauer, die der Übergang benötigt, um beendet zu werden, wird durch die Animation Duration bestimmt.

Sie können eine From/To/By-Animation auf eine Eigenschaft anwenden, indem Sie ein Storyboard Markup und Code verwenden oder die BeginAnimation Methode im Code verwenden. Sie können auch eine From/To/By Animation verwenden, um eine AnimationClock zu erstellen und sie auf eine oder mehrere Eigenschaften anzuwenden. Weitere Informationen zu den verschiedenen Methoden zum Anwenden von Animationen finden Sie unter "Property Animation Techniques Overview".

From/To/By-Animationen können maximal zwei Zielwerte aufweisen. Wenn Sie eine Animation mit mehr als zwei Zielwerten benötigen, verwenden Sie eine Keyframeanimation. Keyframeanimationen werden in der Übersicht überKey-Frame Animationen beschrieben.

Von/Nach/Durch Animationstypen

Da Animationen Eigenschaftswerte generieren, gibt es unterschiedliche Animationstypen für unterschiedliche Eigenschaftstypen. Verwenden Sie zum Animieren einer Eigenschaft, die ein Double benötigt, wie die Width-Eigenschaft eines Elements, eine Animation, die Double-Werte erzeugt. Um eine Eigenschaft zu animieren, die eine Point nutzt, verwenden Sie eine Animation, die Point Werte produziert, und so weiter.

From/To/By-Animationsklassen gehören zum System.Windows.Media.Animation Namespace und verwenden die folgende Benennungskonvention:

<Art>Animation

Wo <Typ> der Typ des Werts ist, den die Klasse animiert.

WPF stellt die folgenden From/To/By-Animationsklassen bereit.

Immobilientyp Entsprechende From/To/By Animationsklasse
Byte ByteAnimation
Color ColorAnimation
Decimal DecimalAnimation
Double DoubleAnimation
Int16 Int16Animation
Int32 Int32Animation
Int64 Int64Animation
Point PointAnimation
Quaternion QuaternionAnimation
Rect RectAnimation
Rotation3D Rotation3DAnimation
Single SingleAnimation
Size SizeAnimation
Thickness ThicknessAnimation
Vector3D Vector3DAnimation
Vector VectorAnimation

Zielwerte

Eine From/To/By-Animation erstellt einen Übergang zwischen zwei Zielwerten. Es ist üblich, einen Startwert anzugeben (mit der From Eigenschaft festlegen) und einen Endwert (mit der To Eigenschaft festlegen). Sie können jedoch auch nur einen Anfangswert, einen Zielwert oder einen Offsetwert angeben. In diesen Fällen ruft die Animation den fehlenden Zielwert aus der Eigenschaft ab, die animiert wird. In der folgenden Liste werden die verschiedenen Methoden zum Angeben der Zielwerte einer Animation beschrieben.

  • Startwert

    Verwenden Sie die From Eigenschaft, wenn Sie den Anfangswert einer Animation explizit angeben möchten. Sie können die From-Eigenschaft allein verwenden oder zusammen mit der To- oder By-Eigenschaft. Wenn Sie nur die From-Eigenschaft angeben, wechselt die Animation von diesem Wert auf den Basiswert der animierten Eigenschaft.

  • Endwert

    Um einen Endwert einer Animation anzugeben, verwenden Sie dessen To Eigenschaft. Wenn Sie die To Eigenschaft allein verwenden, erhält die Animation ihren Startwert entweder aus der Eigenschaft, die animiert wird, oder aus der Ausgabe einer anderen Animation, die auf dieselbe Eigenschaft angewendet wird. Sie können die To Eigenschaft zusammen mit der From Eigenschaft verwenden, um Anfangs- und Endwerte für die Animation explizit anzugeben.

  • Offsetwert

    Mit der By Eigenschaft können Sie einen Offset anstelle eines expliziten Anfangs- oder Endwerts für die Animation angeben. Die By Eigenschaft einer Animation gibt an, wie viel die Animation einen Wert über die Dauer ändert. Sie können die By Eigenschaft selbst oder mit der From Eigenschaft verwenden. Wenn Sie nur die By Eigenschaft angeben, fügt die Animation den Offsetwert zum Basiswert der Eigenschaft oder zur Ausgabe einer anderen Animation hinzu.

Verwenden von Von-/Nach-/Durch-Werten

In den folgenden Abschnitten wird beschrieben, wie Sie die Eigenschaften From, To und By zusammen oder separat verwenden.

In den Beispielen in diesem Abschnitt wird jeweils eine DoubleAnimation-Animation, eine Art From/To/By-Animation, verwendet, um die Width-Eigenschaft eines Rectangle zu animieren, das 10 geräteunabhängige Pixel hoch und 100 geräteunabhängige Pixel breit ist.

Obwohl in jedem Beispiel die DoubleAnimationEigenschaften From, To und By aller From/To/By-Animationen identisch sind. Obwohl jedes dieser Beispiele eine Storyboardverwendet, können Sie From/To/By-Animationen auf andere Weise verwenden. Weitere Informationen finden Sie unter Property Animation Techniques Overview.

Von/nach

Wenn Sie die From- und To-Werte zusammen festlegen, schreitet die Animation von dem Wert fort, der von der From-Eigenschaft angegeben wird, zu dem Wert, der von der To-Eigenschaft angegeben wird.

Im folgenden Beispiel wird die From-Eigenschaft des DoubleAnimation auf 50 und dessen To-Eigenschaft auf 300 festgelegt. Infolgedessen wird die Width der Rectangle von 50 bis 300 animiert.

// Demonstrates the From and To properties used together.

// Create a NameScope for this page so that
// Storyboards can be used.
NameScope.SetNameScope(this, new NameScope());

Rectangle myRectangle = new Rectangle();

// Assign the Rectangle a name so that
// it can be targeted by a Storyboard.
this.RegisterName(
    "fromToAnimatedRectangle", myRectangle);
myRectangle.Height = 10;
myRectangle.Width = 100;
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.Fill = Brushes.Black;

// Demonstrates the From and To properties used together.
// Animates the rectangle's Width property from 50 to 300 over 10 seconds.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 50;
myDoubleAnimation.To = 300;
myDoubleAnimation.Duration =
    new Duration(TimeSpan.FromSeconds(10));

Storyboard.SetTargetName(myDoubleAnimation, "fromToAnimatedRectangle");
Storyboard.SetTargetProperty(myDoubleAnimation,
    new PropertyPath(Rectangle.WidthProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);

// Use an anonymous event handler to begin the animation
// when the rectangle is clicked.
myRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs args)
    {
        myStoryboard.Begin(myRectangle);
    };
' Demonstrates the From and To properties used together.

' Create a NameScope for this page so that
' Storyboards can be used.
NameScope.SetNameScope(Me, New NameScope())

Dim myRectangle As New Rectangle()

' Assign the Rectangle a name so that
' it can be targeted by a Storyboard.
Me.RegisterName("fromToAnimatedRectangle", myRectangle)
myRectangle.Height = 10
myRectangle.Width = 100
myRectangle.HorizontalAlignment = HorizontalAlignment.Left
myRectangle.Fill = Brushes.Black

' Demonstrates the From and To properties used together.
' Animates the rectangle's Width property from 50 to 300 over 10 seconds.
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.From = 50
myDoubleAnimation.To = 300
myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(10))

Storyboard.SetTargetName(myDoubleAnimation, "fromToAnimatedRectangle")
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty))
Dim myStoryboard As New Storyboard()
myStoryboard.Children.Add(myDoubleAnimation)

' Use an anonymous event handler to begin the animation
' when the rectangle is clicked.
AddHandler myRectangle.MouseLeftButtonDown, Sub(sender As Object, args As MouseButtonEventArgs) myStoryboard.Begin(myRectangle)

Bis

Wenn Sie nur die To Eigenschaft setzen, wird die Animation ausgehend vom Grundwert der animierten Eigenschaft oder von der Ausgabe einer zusammengesetzten Animation, die zuvor auf dieselbe Eigenschaft angewendet wurde, hin zu dem von der To Eigenschaft angegebenen Wert ausgeführt.

("Zusammenfügen von Animationen" bezieht sich auf eine Active oder Filling Animation, die zuvor auf dieselbe Eigenschaft angewendet wurde und noch wirksam ist, wenn die aktuelle Animation mithilfe des Compose Übergabeverhaltens angewendet wird.)

Im folgenden Beispiel wird die To-Eigenschaft des DoubleAnimation auf 300 festgelegt. Da kein Anfangswert angegeben wurde, verwendet der DoubleAnimation Basiswert (100) der Width Eigenschaft als Anfangswert. Der Wert des Width in der Rectangle-Animation ändert sich von 100 bis zum Zielwert 300.

// Demonstrates the use of the To property.

// Create a NameScope for this page so that
// Storyboards can be used.
NameScope.SetNameScope(this, new NameScope());

Rectangle myRectangle = new Rectangle();

// Assign the Rectangle a name so that
// it can be targeted by a Storyboard.
this.RegisterName(
    "toAnimatedRectangle", myRectangle);
myRectangle.Height = 10;
myRectangle.Width = 100;
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.Fill = Brushes.Gray;

// Demonstrates the To property used by itself. Animates
// the Rectangle's Width property from its base value
// (100) to 300 over 10 seconds.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.To = 300;
myDoubleAnimation.Duration =
    new Duration(TimeSpan.FromSeconds(10));

Storyboard.SetTargetName(myDoubleAnimation, "toAnimatedRectangle");
Storyboard.SetTargetProperty(myDoubleAnimation,
    new PropertyPath(Rectangle.WidthProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);

// Use an anonymous event handler to begin the animation
// when the rectangle is clicked.
myRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs args)
    {
        myStoryboard.Begin(myRectangle);
    };
' Demonstrates the use of the To property.

' Create a NameScope for this page so that
' Storyboards can be used.
NameScope.SetNameScope(Me, New NameScope())

Dim myRectangle As New Rectangle()

' Assign the Rectangle a name so that
' it can be targeted by a Storyboard.
Me.RegisterName("toAnimatedRectangle", myRectangle)
myRectangle.Height = 10
myRectangle.Width = 100
myRectangle.HorizontalAlignment = HorizontalAlignment.Left
myRectangle.Fill = Brushes.Gray

' Demonstrates the To property used by itself. Animates
' the Rectangle's Width property from its base value
' (100) to 300 over 10 seconds.
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.To = 300
myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(10))

Storyboard.SetTargetName(myDoubleAnimation, "toAnimatedRectangle")
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty))
Dim myStoryboard As New Storyboard()
myStoryboard.Children.Add(myDoubleAnimation)

' Use an anonymous event handler to begin the animation
' when the rectangle is clicked.
AddHandler myRectangle.MouseLeftButtonDown, Sub(sender As Object, args As MouseButtonEventArgs) myStoryboard.Begin(myRectangle)

Bis

Wenn Sie nur die By Eigenschaft einer Animation definieren, entwickelt sich die Animation entweder ausgehend vom Basiswert der animierten Eigenschaft oder ausgehend vom Ergebnis einer zusammengesetzten Animation zur Summe dieses Werts und des Werts, der von der By Eigenschaft angegeben wird.

Im folgenden Beispiel wird die By-Eigenschaft des DoubleAnimation auf 300 festgelegt. Da im Beispiel kein Anfangswert angegeben wird, verwendet der DoubleAnimation Basiswert der Width Eigenschaft 100 als Anfangswert. Der Endwert wird durch Hinzufügen des By Werts der Animation 300 zum Startwert 100: 400 bestimmt. Infolgedessen wird Width des Rectangle von 100 bis 400 animiert.

// Demonstrates the use of the By property.

// Create a NameScope for this page so that
// Storyboards can be used.
NameScope.SetNameScope(this, new NameScope());

Rectangle myRectangle = new Rectangle();

// Assign the Rectangle a name so that
// it can be targeted by a Storyboard.
this.RegisterName(
    "byAnimatedRectangle", myRectangle);
myRectangle.Height = 10;
myRectangle.Width = 100;
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.Fill = Brushes.RoyalBlue;

// Demonstrates the By property used by itself.
// Increments the Rectangle's Width property by 300 over 10 seconds.
// As a result, the Width property is animated from its base value
// (100) to 400 (100 + 300) over 10 seconds.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.By = 300;
myDoubleAnimation.Duration =
    new Duration(TimeSpan.FromSeconds(10));

Storyboard.SetTargetName(myDoubleAnimation, "byAnimatedRectangle");
Storyboard.SetTargetProperty(myDoubleAnimation,
    new PropertyPath(Rectangle.WidthProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);

// Use an anonymous event handler to begin the animation
// when the rectangle is clicked.
myRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs args)
    {
        myStoryboard.Begin(myRectangle);
    };
' Demonstrates the use of the By property.

' Create a NameScope for this page so that
' Storyboards can be used.
NameScope.SetNameScope(Me, New NameScope())

Dim myRectangle As New Rectangle()

' Assign the Rectangle a name so that
' it can be targeted by a Storyboard.
Me.RegisterName("byAnimatedRectangle", myRectangle)
myRectangle.Height = 10
myRectangle.Width = 100
myRectangle.HorizontalAlignment = HorizontalAlignment.Left
myRectangle.Fill = Brushes.RoyalBlue

' Demonstrates the By property used by itself.
' Increments the Rectangle's Width property by 300 over 10 seconds.
' As a result, the Width property is animated from its base value
' (100) to 400 (100 + 300) over 10 seconds.
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.By = 300
myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(10))

Storyboard.SetTargetName(myDoubleAnimation, "byAnimatedRectangle")
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty))
Dim myStoryboard As New Storyboard()
myStoryboard.Children.Add(myDoubleAnimation)

' Use an anonymous event handler to begin the animation
' when the rectangle is clicked.
AddHandler myRectangle.MouseLeftButtonDown, Sub(sender As Object, args As MouseButtonEventArgs) myStoryboard.Begin(myRectangle)

Von/nach

Wenn Sie die From- und By-Eigenschaften einer Animation festlegen, schreitet die Animation vom Wert, der durch die From-Eigenschaft angegeben wird, zu dem Wert voran, der durch die Summe der From- und By-Eigenschaften bestimmt wird.

Im folgenden Beispiel wird die From-Eigenschaft des DoubleAnimation auf 50 und dessen By-Eigenschaft auf 300 festgelegt. Der Endwert wird durch Hinzufügen des By Werts der Animation 300 zum Startwert 50: 350 bestimmt. Infolgedessen wird der Width des Rectangle von 50 bis 350 animiert.

// Demonstrates the use of the From and By properties.

// Create a NameScope for this page so that
// Storyboards can be used.
NameScope.SetNameScope(this, new NameScope());

Rectangle myRectangle = new Rectangle();

// Assign the Rectangle a name so that
// it can be targeted by a Storyboard.
this.RegisterName(
    "byAnimatedRectangle", myRectangle);
myRectangle.Height = 10;
myRectangle.Width = 100;
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.Fill = Brushes.BlueViolet;

// Demonstrates the From and By properties used together.
// Increments the Rectangle's Width property by 300 over 10 seconds.
// As a result, the Width property is animated from 50
// to 350 (50 + 300) over 10 seconds.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 50;
myDoubleAnimation.By = 300;
myDoubleAnimation.Duration =
    new Duration(TimeSpan.FromSeconds(10));

Storyboard.SetTargetName(myDoubleAnimation, "byAnimatedRectangle");
Storyboard.SetTargetProperty(myDoubleAnimation,
    new PropertyPath(Rectangle.WidthProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);

// Use an anonymous event handler to begin the animation
// when the rectangle is clicked.
myRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs args)
    {
        myStoryboard.Begin(myRectangle);
    };
' Demonstrates the use of the From and By properties.

' Create a NameScope for this page so that
' Storyboards can be used.
NameScope.SetNameScope(Me, New NameScope())

Dim myRectangle As New Rectangle()

' Assign the Rectangle a name so that
' it can be targeted by a Storyboard.
Me.RegisterName("byAnimatedRectangle", myRectangle)
myRectangle.Height = 10
myRectangle.Width = 100
myRectangle.HorizontalAlignment = HorizontalAlignment.Left
myRectangle.Fill = Brushes.BlueViolet

' Demonstrates the From and By properties used together.
' Increments the Rectangle's Width property by 300 over 10 seconds.
' As a result, the Width property is animated from 50
' to 350 (50 + 300) over 10 seconds.
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.From = 50
myDoubleAnimation.By = 300
myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(10))

Storyboard.SetTargetName(myDoubleAnimation, "byAnimatedRectangle")
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty))
Dim myStoryboard As New Storyboard()
myStoryboard.Children.Add(myDoubleAnimation)

' Use an anonymous event handler to begin the animation
' when the rectangle is clicked.
AddHandler myRectangle.MouseLeftButtonDown, Sub(sender As Object, args As MouseButtonEventArgs) myStoryboard.Begin(myRectangle)

Von

Wenn Sie nur den From-Wert einer Animation angeben, ändert sich die Animation vom Wert, der von der From-Eigenschaft angegeben wird, zum Basiswert der animierten Eigenschaft oder zum Output einer komponierenden Animation.

Im folgenden Beispiel wird nur die From Eigenschaft von DoubleAnimation auf 50 festgelegt. Da kein Endwert angegeben wurde, verwendet der DoubleAnimation Basiswert der Width Eigenschaft 100 als Endwert. Der Width des Rectangle wird von 50 bis zum Basiswert der Width-Eigenschaft, nämlich 100, animiert.

// Demonstrates the use of the From property.

// Create a NameScope for this page so that
// Storyboards can be used.
NameScope.SetNameScope(this, new NameScope());

Rectangle myRectangle = new Rectangle();

// Assign the Rectangle a name so that
// it can be targeted by a Storyboard.
this.RegisterName(
    "fromAnimatedRectangle", myRectangle);
myRectangle.Height = 10;
myRectangle.Width = 100;
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.Fill = Brushes.Purple;

// Demonstrates the From property used by itself. Animates the
// rectangle's Width property from 50 to its base value (100)
// over 10 seconds.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 50;
myDoubleAnimation.Duration =
    new Duration(TimeSpan.FromSeconds(10));

Storyboard.SetTargetName(myDoubleAnimation, "fromAnimatedRectangle");
Storyboard.SetTargetProperty(myDoubleAnimation,
    new PropertyPath(Rectangle.WidthProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);

// Use an anonymous event handler to begin the animation
// when the rectangle is clicked.
myRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs args)
    {
        myStoryboard.Begin(myRectangle);
    };
' Demonstrates the use of the From property.

' Create a NameScope for this page so that
' Storyboards can be used.
NameScope.SetNameScope(Me, New NameScope())

Dim myRectangle As New Rectangle()

' Assign the Rectangle a name so that
' it can be targeted by a Storyboard.
Me.RegisterName("fromAnimatedRectangle", myRectangle)
myRectangle.Height = 10
myRectangle.Width = 100
myRectangle.HorizontalAlignment = HorizontalAlignment.Left
myRectangle.Fill = Brushes.Purple

' Demonstrates the From property used by itself. Animates the
' rectangle's Width property from 50 to its base value (100)
' over 10 seconds.
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.From = 50
myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(10))

Storyboard.SetTargetName(myDoubleAnimation, "fromAnimatedRectangle")
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty))
Dim myStoryboard As New Storyboard()
myStoryboard.Children.Add(myDoubleAnimation)

' Use an anonymous event handler to begin the animation
' when the rectangle is clicked.
AddHandler myRectangle.MouseLeftButtonDown, Sub(sender As Object, args As MouseButtonEventArgs) myStoryboard.Begin(myRectangle)

An/Durch

Wenn Sie sowohl die To Eigenschaften einer Animation als auch die By Eigenschaften einer Animation festlegen, wird die By Eigenschaft ignoriert.

Andere Animationstypen

From/To/By-Animationen sind nicht die einzige Art von Animationen, die von WPF bereitgestellt werden: Außerdem werden Keyframeanimationen und Pfadanimationen bereitgestellt.

Mit WPF können Sie auch eigene benutzerdefinierte Animationstypen erstellen. Weitere Informationen finden Sie in der Übersicht über benutzerdefinierte Animationen.

Siehe auch