针对WP8的GridLength的故事板

本文关键字:的故事 GridLength WP8 针对 | 更新日期: 2023-09-27 18:10:25

我想为RowDefinition更改高度制作一个故事板,我发现这对我很有帮助。唯一的问题,当我想创建类GridLengthAnimation,我不能使它成为AnimationTimeline。这是因为windows phone 8不支持这个吗?

在这种情况下,是否有另一种工作围绕为RowDefinition制作故事板?

针对WP8的GridLength的故事板

最简单的方法可能是将网格放到行中,并像这样动画化它们的height属性。

这里是xaml:
<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid Background="AliceBlue"
          Grid.Row="0"
          Height="100"
          Tap="Grid_Tap"
          CacheMode="BitmapCache" />
    <Grid Background="AntiqueWhite"
          Grid.Row="1"
          Height="100"
          Tap="Grid_Tap"
          CacheMode="BitmapCache" />
    <Grid Background="Aqua"
          Grid.Row="2"
          Height="100"
          Tap="Grid_Tap"
          CacheMode="BitmapCache" />
    <Grid Background="Aquamarine"
          Grid.Row="3"
          Height="100"
          Tap="Grid_Tap"
          CacheMode="BitmapCache" />
</Grid>

和cs:

private void AnimateHeight(Grid grid)
{
    double newHeight = grid.ActualHeight == 100 ? 300 : 100; //select the height we want to animate
    Storyboard story = new Storyboard();
    DoubleAnimation animation = new DoubleAnimation();
    animation.To = newHeight;
    animation.Duration = TimeSpan.FromSeconds(0.5);
    Storyboard.SetTarget(animation, grid);
    Storyboard.SetTargetProperty(animation, new PropertyPath(Grid.HeightProperty));
    story.Children.Add(animation);
    story.Begin();
}
private void Grid_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    Grid grid = sender as Grid; //select the grid we tapped
    AnimateHeight(grid);
}

注意,我将缓存模式设置为bitmapcache所有网格。这并不是必需的,但是会使动画更加流畅,因为静态网格不会在每一帧中重新绘制。