Why does my storyboard work only once?

I have a storyboard that I would like to run several times. Unfortunately, after its launch for the first time, even when this code got in, it does not revive again. It only revives the first time. I set the initial value .... even when I set the value before we run it again.

DoubleAnimationUsingKeyFrames animateTransformX = new DoubleAnimationUsingKeyFrames();

EasingDoubleKeyFrame initialTransformXKeyframe = new EasingDoubleKeyFrame();
initialTransformXKeyframe.KeyTime = TimeSpan.FromSeconds(0);
//Resetting the value before running it again.
initialTransformXKeyframe.Value = 0;
animateTransformX.KeyFrames.Add(initialTransformXKeyframe);


EasingDoubleKeyFrame animateTransformXKeyframe = new EasingDoubleKeyFrame();
animateTransformXKeyframe.KeyTime = TimeSpan.FromSeconds(0.5);
animateTransformXKeyframe.Value = aDynamicValueGoesHere; 
animateTransformX.KeyFrames.Add(animateTransformXKeyframe);


Storyboard.SetTarget(animateTransformX, image);
Storyboard.SetTargetProperty(animateTransformX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)"));

Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(animateTransformX);

myStoryboard.Begin(this);

It seems to me that it is very simple, but for me life I can not understand why. Any help would be appreciated.

EDIT

The reason I did this with code is because the storyboards are frozen, and in the past I had problems with dynamic values ​​in them. For simplicity, I did not believe that I used a dynamic value in code; I updated the code above to show that I am using a dynamic value.

XAML . ( ), , ( ) .

, {Binding OffsetX} 50, , . OffsetX , INotifyPropertyChanged. , , , , , ( 50). ; . .

<Storyboard x:Key="InitialAnimate">
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)" Storyboard.TargetName="StoryImage">
        <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="{Binding OffsetX}"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

2

, , . :

System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='Stop'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='28715394'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='System.Windows.Controls.Grid'; TargetElement.HashCode='6255521'; TargetElement.Type='System.Windows.Controls.Grid'

StackOverflow , , , Storyboard , .

, , . (, ), , , , UserControl, ,

UserControl ControlStoryboardAction, Loaded EventTrigger. , , - UserControl. XAML. parent:

:

<grid x:Name="imageHolder></grid>

:

private void CreateAnimations(object sender, EventArgs e)
{
    imageHolder.Children.RemoveAt(0);
    Views.Image image = new Image();
    image.Name = "image";
    imageHolder.Children.Add(image);
}
+4
1

Value , .

, - . MSDN, , , (.. ) .

, .

. Storyboard ?

, , , codebehind.

, , , :

  1. ( ) .
0

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


All Articles