WPF animation issue when using Storyboard from code

I am working on a 3D carousel of flat, square tiles that will contain information. I am working on animating this carousel to rotate when a person clicks the Next and Previous buttons.

I got it to work using BeginAnimation on the Rotation property for RotateTransform3D, which I applied to the carousel, but I can’t imagine the storyboard version of the same animation work. The reason I need the storyboard version is the HandOffBehavior.Compose parameter, because without it a few clicks of my next and previous buttons lead to an inconsistent carousel.

Here is the code for the storyboard:

RotateTransform3D tempTransform = (RotateTransform3D)wheel.Transform;
AxisAngleRotation3D rotation = (AxisAngleRotation3D)tempTransform.Rotation;

Storyboard storyboard = new Storyboard();            
DoubleAnimation animation = new DoubleAnimation();
animation.By = defaultAngle;
animation.Duration = TimeSpan.FromSeconds(.5);

Storyboard.SetTarget(animation, rotation);
Storyboard.SetTargetProperty(animation, new PropertyPath("Angle"));
storyboard.Children.Add(animation);

storyboard.Duration = animation.Duration;            
storyboard.Begin(new FrameworkContentElement(), HandoffBehavior.Compose);

- . , , . . BeginAnimation, HandOffBehavior.Compose.

+3
1

2D-, , .

- (, XAML), Storyboards Freezable, . (. .) , , Storyboard.SetTarget(, ), , .

:

  • , .
  • Call RegisterName() Freezable.
  • Storyboard.Begin()

( ):

FrameworkContentElement element = new FrameworkContentElement();
NameScope.SetNameScope(element, new NameScope());

RotateTransform3D tempTransform = (RotateTransform3D)wheel.Transform;
AxisAngleRotation3D rotation = (AxisAngleRotation3D)tempTransform.Rotation;
element.RegisterName("rotation", rotation);

Storyboard storyboard = new Storyboard();            
DoubleAnimation animation = new DoubleAnimation();
animation.By = defaultAngle;
animation.Duration = TimeSpan.FromSeconds(.5);

Storyboard.SetTarget(animation, rotation);
Storyboard.SetTargetProperty(animation, new PropertyPath("Angle"));
storyboard.Children.Add(animation);

storyboard.Duration = animation.Duration;            
storyboard.Begin(element, HandoffBehavior.Compose);

XAML, .

EDIT: , , :

var T = new TranslateTransform(40, 0);
Duration duration = new Duration(new TimeSpan(0, 0, 0, 1, 0);
DoubleAnimation anim = new DoubleAnimation(30, duration);
T.BeginAnimation(TranslateTransform.YProperty, anim);
+1

Source: https://habr.com/ru/post/1757446/


All Articles