But for some reason, animating motion (like: move my text box off screen) is a pain. Here are the important steps.
- Give your object an x:Name (“obj”). Let’s also assume that the object is a custom object of type local:JokeControl. The word local refers to a XML namespace definition.
- Add a RenderTransform with a TranslateTransform child to your object
- Add in a storyboard and give it an x:Naem
- Set the storyboard’s TargetName to the object name
- Set the storyboard’s TargetProperty using the weird ‘path’ syntax: (local:JokeControl.RenderTransform).(TranslateTransform.X)
Here are the actual bits of code:
From the XAML file:
Notes | Code |
Set up the ‘local’ namespace so that my user control (which is in C# namespace ‘Jokes’) can be used. | xmlns:local="using:Jokes" |
Create the animation in the resource section. The storyboard (animation) is named uiAnimateJokeOut, and actually has more bits than this. | <Grid.Resources> <Storyboard x:Name="uiAnimateJokeOut" > <DoubleAnimation Storyboard.TargetName="uiJokeControl" Storyboard.TargetProperty="(local:JokeControl.RenderTransform).(TranslateTransform.X)" From="0.0" To="400.0" Duration="0:0:0.5" AutoReverse="False" /> |
The object I want to animate needs a RenderTransform; the particular render transform I need is a Translate transform. I don’t have to name it because I’ll find it by type. | <local:JokeControl x:Name="uiJokeControl" Grid.Column="1" > <local:JokeControl.RenderTransform> <TranslateTransform X="0" > </TranslateTransform> </local:JokeControl.RenderTransform> </local:JokeControl> |
C# code to start the animation | uiAnimateJokeOut.Begin(); |
The code looks like this:
uiAnimateJokeOut.Completed += (s, args) => { DoRandom(); uiAnimateJokeIn.Begin(); };