Antonio
Antonio

Reputation: 1231

Start Storyboard from C# code

I am trying to call a storyboard declared in xaml code from C#:

<Storyboard x:Key="BotRotation"  Duration="00:00:4" RepeatBehavior="Forever" >
            <DoubleAnimation BeginTime="0:0:0"
            Storyboard.TargetName="imageRotateTransformm"
            Storyboard.TargetProperty="ScaleX"
            From="1" To="-1"
            Duration="0:0:2"   
            />
            <DoubleAnimation
            BeginTime="0:0:2"
            Storyboard.TargetName="imageRotateTransformm"
            Storyboard.TargetProperty="ScaleX"
            From="-1" To="1"
            Duration="0:0:2"  
            />
        </Storyboard>
</Window.Resources>

This storyboard should modify the ScaleX property of image. Image Declaration:

<Image Name="uiRobotIcon" Height="64" Width="64" Source="/YoutubeTelegramAudio;component/imgs/ic_robot.png" RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleY="1" ScaleX="1" x:Name="imageRotateTransformm" />
                    <SkewTransform AngleY="0" AngleX="0" />
                    <RotateTransform Angle="0" />
                    <TranslateTransform/>
                </TransformGroup>
            </Image.RenderTransform>
        </Image>

Then, i want to start this animation from C# when i click another button. Thanks.

Upvotes: 4

Views: 10212

Answers (4)

The rigjh
The rigjh

Reputation: 67

I found a solution in code project: https://www.codeproject.com/Articles/364529/Animation-using-Storyboards-in-WPF

First add the tags:

<Window.Resources> </Window.Resources>

Now you can create your storyboard:

     <Storyboard x: Key = "yourSb">
         <DoubleAnimation Storyboard.TargetName = "_ window"
       Storyboard.TargetProperty = "Opacity" From = "1" To = "0.3"
       Duration = "0: 0: 1" />
     </Storyboard>

Don't forget to add 'x: key = "yourSb"' 'at the beginning inside your StoryBoard tag.

And in your code-behind:

       Storyboard s = (Storyboard)TryFindResource("yourSb");
        s.Begin();

Upvotes: 0

Sebastian &#208;ymel
Sebastian &#208;ymel

Reputation: 717

You can also do it in pure xaml, just use trigger action:

    <Button>
        <Button.Triggers>
            <EventTrigger RoutedEvent="PreviewMouseDown">
                <BeginStoryboard  Storyboard="{DynamicResource BotRotation}"/>
            </EventTrigger>
        </Button.Triggers>
    </Button>

Upvotes: 1

Antonio
Antonio

Reputation: 1231

I found the solution. First I have included the storyboard tag into BeginStoryBoard:

<Window.Resources>
    <BeginStoryboard x:Key="BotRotation">
        <Storyboard   Duration="00:00:4" RepeatBehavior="Forever" >
            <DoubleAnimation BeginTime="0:0:0"
            Storyboard.TargetName="imageRotateTransformm"
            Storyboard.TargetProperty="ScaleX"
            From="1" To="-1"
            Duration="0:0:2"   
            />
            <DoubleAnimation
            BeginTime="0:0:2"
            Storyboard.TargetName="imageRotateTransformm"
            Storyboard.TargetProperty="ScaleX"
            From="-1" To="1"
            Duration="0:0:2"  
            />
        </Storyboard>
    </BeginStoryboard>
</Window.Resources>

Finally i call the animation from C# with this statement:

BeginStoryboard sb = this.FindResource("BotRotation") as BeginStoryboard;
sb.Storyboard.Begin();

Upvotes: 4

Fᴀʀʜᴀɴ Aɴᴀᴍ
Fᴀʀʜᴀɴ Aɴᴀᴍ

Reputation: 6251

That's very easy. Just find the Resource, cast it to a Storyboard and then call its Begin() method:

Storyboard sb = (<YourNamespace>.Properties.Resources["BotRotation"] as Storyboard);
sb.Begin();

Put the above code in some event handler and it should work fine.

But:

It also matters where in the application you declare the Storyboard. If it is in the App.xaml, then no worries but anywhere else, and it might be unaccessible. But I think the Window.Resources tag can also contain accessible resources. I can't test it right now :)

Upvotes: 4

Related Questions