在我的XAML文件中有一个网格: `
<Grid x:Name="mainGrid" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" >
</Grid>
`
让我们说,它包含许多对象 - 图像,文本等。 问题是 - 如何仅使用后面的代码来动画这个网格(位置)?
答案 0 :(得分:3)
这是一个简单的Sliding animation
XAML部分
<Grid x:Name="Gridg" Height="200" Width="200" >
<Grid.RenderTransform>
<CompositeTransform ></CompositeTransform>
</Grid.RenderTransform>
</Grid>
代码隐藏:在任何按钮点击事件
private void StartAnimation(object sender, RoutedEventArgs e)
{
Storyboard moveSb=new Storyboard();
TranslateTransform moveTransform = new TranslateTransform();
Gridg.RenderTransform = moveTransform;
Duration duration = new Duration(TimeSpan.FromSeconds(2));
DoubleAnimation myDoubleAnimationX = new DoubleAnimation();
myDoubleAnimationX.Duration = duration;
myDoubleAnimationX.To = 200;
moveSb.Children.Add(myDoubleAnimationX);
Storyboard.SetTarget(myDoubleAnimationX, moveTransform);
Storyboard.SetTargetProperty(myDoubleAnimationX, "X");
moveSb.Begin();
}