Aligoglos
Aligoglos

Reputation: 93

How Run Xaml Animation With C# Code

I Have Animation In My ResourceDictionary But I Want Call Animation In My C# Code. This ResourceDictionary Add To My Resources App

Example:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<BeginStoryboard x:Key="PuzzleFall">
                <Storyboard Storyboard.TargetName="RenderT" Storyboard.TargetProperty="X">
                    <DoubleAnimationUsingKeyFrames  Duration="0:0:3" BeginTime="0:0:0">
                        <SplineDoubleKeyFrame Value="0" KeySpline="1 0,0.95 0" KeyTime="0:0:3"/>
                    </DoubleAnimationUsingKeyFrames>

                    <DoubleAnimationUsingKeyFrames BeginTime="0:0:3.06" Duration="0:0:1.5">
                        <SplineDoubleKeyFrame Value="50" KeySpline="0 1,0 1" KeyTime="0:0:1.5"/>
                    </DoubleAnimationUsingKeyFrames>

                    <DoubleAnimationUsingKeyFrames BeginTime="0:0:3.08" Duration="0:0:1">
                        <SplineDoubleKeyFrame Value="0" KeySpline="1 0,1 0" KeyTime="0:0:1"/>
                    </DoubleAnimationUsingKeyFrames>

                    <DoubleAnimationUsingKeyFrames BeginTime="0:0:4.1" Duration="0:0:1">
                        <SplineDoubleKeyFrame Value="15" KeySpline="0 1,0 1" KeyTime="0:0:1"/>
                    </DoubleAnimationUsingKeyFrames>

                    <DoubleAnimationUsingKeyFrames BeginTime="0:0:4.15" Duration="0:0:0.5">
                        <SplineDoubleKeyFrame Value="0" KeySpline="1 0,1 0" KeyTime="0:0:0.5"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>

</BeginStoryboard>
</ResourceDictionary> 

Then I Want Run This Animation In C# Code.

Thanks.

Upvotes: 1

Views: 3558

Answers (1)

brunnerh
brunnerh

Reputation: 184296

You probably should not put the Storyboard which is the main component inside a BeginStoryboard especially if you use code. EIther way. you can get the Storyboard using FindResource.

In your current setup something like this:

var beginsb = (BeginStoryboard)FindResource("PuzzleFall");
var sb = beginsb.Storyboard;
sb.Begin();

Upvotes: 4

Related Questions