【问题标题】:Window Store App: Grid animation from code behind? [closed]Window Store App:来自代码背后的网格动画? [关闭]
【发布时间】:2013-12-02 12:22:05
【问题描述】:
在我的 XAML 文件中有一个网格:
`
<Grid x:Name="mainGrid" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" >
</Grid>
`
假设它包含许多对象 - 图像、文本等。
问题是 - 如何仅使用后面的代码为这个网格(位置)设置动画?
【问题讨论】:
标签:
c#
grid
microsoft-metro
windows-store-apps
【解决方案1】:
这是一个简单的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();
}
详情请看Storyboard Animation