tags:

views:

1295

answers:

5

Hi!

I am looking for an easy way to convert some XAML animation to C# code. Is there any tool available to do that?

Thanks!

Wally

This is the code I am trying to convert:

<Storyboard x:Key="Storyboard1">
  <Rotation3DAnimationUsingKeyFrames BeginTime="00:00:00"
      Storyboard.TargetName="DefaultGroup"
      Storyboard.TargetProperty="(Visual3D.Transform).(Transform3DGroup.Children)[2].(RotateTransform3D.Rotation)">
    <SplineRotation3DKeyFrame KeyTime="00:00:04.0200000">
      <SplineRotation3DKeyFrame.Value>
        <AxisAngleRotation3D Angle="144.99999999999997" Axis="1,0,0"/>
      </SplineRotation3DKeyFrame.Value>
    </SplineRotation3DKeyFrame>
  </Rotation3DAnimationUsingKeyFrames>
</Storyboard>
+1  A: 

You could use XamlReader.Load("") to parse it. Hope it helps :)

Edit:

StoryBoard myStoryBoard = (StoryBoard)XamlReader.Load("code");
gsnerf
Thanks for the answer , it works but I am trying to modify some values at runtime and I think I need direct access to the variables involved in the animation, I will test it...
You should be able to access the storyboards content through the children collection (myStoryBoard.Children[0] should be your rotation).You could also try to create it completely in codebehind, see: http://social.expression.microsoft.com/Forums/en-US/wpf/thread/f442644f-a6e1-47f1-8d6f-f65ab3949eee
gsnerf
A: 

Apparently XamlIt will do this. Haven't tried it though: http://www.xamlt.com/

Ilya Tchivilev
+1  A: 

Why do you need to convert the XAML to procedural code? If I read one of your comments correctly it's because you need to change values at runtime. If this is the case, data binding may be better. Could you describe what you're trying to do with the C# that you can't do with XAML?

Neil Williams
A: 

Hi
I am trying to display numbers with animations of a custom cylinder wrapped with an image of the numbers from 0 to 9,
and loop according to an int variable,
I wanted to translate the xaml code in order to create a funcion to trigger the animation
but create the animation dinamically, according to the current rotation angle and the one needed ,
this up to how many numbers were added to my integer.

thanks

+1  A: 

This is the same code in c#:

Storyboard storyboard3 = new Storyboard(); storyboard3.AutoReverse = false; Rotation3DAnimationUsingKeyFrames r3d = new Rotation3DAnimationUsingKeyFrames(); r3d.BeginTime = new TimeSpan(0, 0, 0, 0, 0); r3d.Duration = new TimeSpan(0,0,0,1,0);

        r3d.KeyFrames.Add(new SplineRotation3DKeyFrame(new AxisAngleRotation3D(new Vector3D(1,0,0),1)));
        r3d.KeyFrames.Add(new SplineRotation3DKeyFrame(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 37)));
        r3d.Name = "r3d";

        Storyboard.SetTargetName(r3d, "DefaultGroup");
        Storyboard.SetTargetProperty(r3d, new PropertyPath("(Visual3D.Transform).(Transform3DGroup.Children)[2].(RotateTransform3D.Rotation)")); 

        storyboard3.Children.Add(r3d);
        this.BeginStoryboard(storyboard3);