4
votes

I would like to create dynamic Translate Y animations for my grids, but I can't find how to do it in UWP programmatically.

I have this code, but it says

WinRT information: Cannot resolve TargetProperty TranslateY on specified object.

I have tried to set property name to Y, but it says:

WinRT information: Cannot resolve TargetProperty Y on specified object.

Sample:

private void CreateStoryBoardAnimation(Grid myGrid)
{
    myGrid.RenderTransform = new CompositeTransform();

    Storyboard storyboard = new Storyboard();

    DoubleAnimation translateYAnimation = new DoubleAnimation();
    translateYAnimation.From = -500;
    translateYAnimation.To = 1;
    translateYAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(500));

    Storyboard.SetTarget(translateYAnimation, myGrid);
    Storyboard.SetTargetProperty(translateYAnimation, "TranslateY");

    storyboard.Children.Add(translateYAnimation);

    storyboard.Begin();
} 

I have also tried to use TranslateTransform class... But I don't get how could i use it.

Storyboard.SetTargetProperty only accepts a string in UWP.

I would like to move my grid on screen with animation. I know how to make a storyboard animation in blend, but that is not an option in this case.

2

2 Answers

5
votes

Your code is totally true only use this line :

Storyboard.SetTargetProperty(translateYAnimation, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)");

instead of

Storyboard.SetTargetProperty(translateYAnimation, "TranslateY");

That's it . Have a great day :)

1
votes

I would suggest you to define animation in xaml and update all required parameters in code:

<Grid>
    <Grid.Resources>
        <Storyboard x:Name="SlidingAnimatoin">
            <DoubleAnimation Storyboard.TargetName="TranslateTransform"
                             Storyboard.TargetProperty="Y"
                             From="100"
                             To="0"
                             Duration="0:0:0.5">
            </DoubleAnimation>
        </Storyboard>
    </Grid.Resources>
    <Grid.RenderTransform>
        <TranslateTransform x:Name="TranslateTransform"/>
    </Grid.RenderTransform>
</Grid>

In code you can access storyboard by its name SlidingAnimatoin and update any properties even in DoubleAnimation.