Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have this code in XAML but I want to translate it to code behind. I already search on the internet how to convert from xaml to code, but from what I seen, there is some difference on how the xaml itself such as moving, rotate, color changing etc with different approach. How exactly we can read the animation from xaml to code behind?
XML
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.StrokeDashOffset)" Storyboard.TargetName="path19">
    <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
    <EasingDoubleKeyFrame KeyTime="0:0:1" Value="-14"/>
</DoubleAnimationUsingKeyFrames>


What are first step in doing in code behind and follow by what code after that based from xaml?
Posted

I believe the first step is searching online to find the classes that are used in the animation.

I have not tried this, but the following is how I think it would work. :-)

C#
DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames();
KeyTime start = new TimeSpan(0);
animation.KeyFrames.Add(new EasingDoubleKeyFrame(0, start));
KeyTime end = TimeSpan.FromSeconds(1);
animation.KeyFrames.Add(new EasingDoubleKeyFrame(-14,end));
            
[YourShapeMember].BeginAnimation(Shape.StrokeDashOffsetProperty, animation);
 
Share this answer
 
Comments
Luiey Ichigo 7-Dec-15 11:59am    
Yeah man, its work. Thanks. But I made a little bit modification which combines the with yours:-
Dim mypath As New Shapes.Path()
Dim offset_animation As New DoubleAnimation()

offset_animation.From = 0
offset_animation.To = -14
offset_animation.Duration = New Duration(TimeSpan.FromSeconds(1))
offset_animation.RepeatBehavior = RepeatBehavior.Forever
mypath.StrokeDashOffset = ("-14")
mypath.BeginAnimation(Shape.StrokeDashOffsetProperty, offset_animation)
I want to give you the very general answer and one practical advice.

You can always re-implement any given XAML behavior to code behind. Pretty obviously, the other way around is not true. You can understand it if you learn how XAML works: the build uses XAML data to generate some code behind. This auto-generated code then goes to a compiler, exactly as another other piece of code. (Of course, you can always use some XAML purely during runtime. This is just not the case related to your question.)

So, here is my general-purpose practical advice: suppose you learned how to achieve some behavior in XAML but you want to implement something in code behind, something based on this XAML behavior, and you don't know how. You can easily learn what's going on in this simple way: the auto-generated files are preserved after the build; you will easily find them: the sub-directory "obj" is created by build, it is a sub-directory of the directory of your project files; and all the auto-generated files are in the directory structure of "obj".

For example: let's say you use C# and XAML, and you create with XAML just three classes: Window classes "WindowA" and "WindowB", and also a UserControl class "ControlC". Then, in auto-generated code, you will be able to locate the files "WindowA.g.cs", "WindowB.g.cs" and "ControlC.g.cs". Look at them.

They are the files written in your development language (C#, VB.NET, C++/CLI, F# and the like). Open then and see what they do. This way, you can learn what happens. Usually, the code is more complicated than the code you would really need in your code behind, but for you, it's only important to get the idea: what FCL classes and what members to use to achieve what you want.

—SA
 
Share this answer
 
v2
Comments
Luiey Ichigo 7-Dec-15 21:02pm    
I'm actually made all the animation in Blend. To apply 1 animation that animate about 200 paths made the computer lag(animate all but showing 1path only depends on user selection on previous page). So, I want to made it in code to perform animation on specific path only.
Sergey Alexandrovich Kryukov 8-Dec-15 0:26am    
Not that you mentioned Blend in your original question, but XAML is XAML and code behind is code behind, and Blend is just a different development tool. The principles are the same...

—SA
Luiey Ichigo 8-Dec-15 22:29pm    
Oh..apologize for not mention it before. actually I'm designing the page using Blend and control it using VS. Because of running all animation and show only one path, that makes the system lag, that's why I'm deciding to put the animation on code rather than make it trigger on page load. Thanks for your explanation under solutions. Appreciate it
Sergey Alexandrovich Kryukov 8-Dec-15 22:32pm    
No problem. Will you accept the answer formally then?
This is a general approach you can use in many cases.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900