我正在尝试创建一个简单的动画,其中栅格从0高度到设置值320进行动画处理(这将产生幻灯片感觉),同时还对0-1和后向的不透明度进行动画处理。
我创建了两个情节提要,并给它们起了个名字,它们是:
<Page.Resources>
<Storyboard x:Name="OpenSettings" Storyboard.TargetName="SettingGrid">
<DoubleAnimation Storyboard.TargetProperty="Height" Storyboard.TargetName="SettingGrid" To="320"/>
<DoubleAnimation Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SettingGrid" To="1"/>
</Storyboard>
<Storyboard x:Name="CloseSettings">
<DoubleAnimation Storyboard.TargetProperty="Height" Storyboard.TargetName="SettingGrid" To="0"/>
<DoubleAnimation Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SettingGrid" To="0"/>
</Storyboard>
</Page.Resources>
然后,我尝试从代码后台运行动画,如下所示:
private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
if (settingsOpened)
{
CloseSettings.Begin();
settingsOpened = false;
}
else
{
OpenSettings.Begin();
settingsOpened = true;
}
}
bool settingsOpened = false;
问题在于高空动画。它不会更改网格上的任何高度值。顺便说一下,不透明度动画效果很好。
有谁知道我可能能够解决这个问题?
这也是我要设置动画的网格。请注意,我如何设置固定的起始值,这使得动画中的“ from”属性不再必要。
<Grid Opacity="1" Height="0" VerticalAlignment="Top" Name="SettingGrid" Background="#151515">
<CheckBox Margin="10,0,0,0" Content="Use front camera" Height="80"/>
<TextBlock Text="IP Address" Margin="10,70,10,0" FontSize="25" FontWeight="Light" Height="30" VerticalAlignment="Top"/>
<TextBox Text="127.0.0.1" Margin="10,100,10,0" Height="40" VerticalAlignment="Top" Name="IPBox"/>
<TextBlock Text="Port" Margin="10,150,10,0" FontSize="25" FontWeight="Light" Height="30" VerticalAlignment="Top"/>
<TextBox Text="12345" Margin="10,180,10,0" Height="40" VerticalAlignment="Top" Name="PortBox"/>
<Slider Margin="10,230,10,0" Height="45" VerticalAlignment="Top" Name="updateFreq" Value="20"/>
<StackPanel Margin="10,270,0,0" Orientation="Horizontal">
<TextBlock FontSize="25" VerticalAlignment="Top" Margin="10,0,0,0" FontWeight="Light" HorizontalAlignment="Left" Text="{Binding ElementName=updateFreq, Path=Value}"/>
<TextBlock Text=" Times per second" FontSize="25" FontWeight="Light" VerticalAlignment="Top"/>
</StackPanel>
</Grid>
就像我说的,为什么动画没有做任何事情?
最佳答案
无需为Height设置动画,而是将RenderTransform添加到SettingGrid并将其设置为从0到1.0的ScaleTransform.ScaleY动画。另一种选择是使用一种内置的个性过渡而不是自定义动画(请参见Quickstart: Animating your UI using library animations (XAML))
动画高度会影响场景的布局,并且需要与主UI线程同步。
这称为“相关动画”。对RenderTransform进行动画处理将产生非常相似的效果,但是不需要额外的布局传递,并且可以在渲染线程上完全运行。这称为“独立动画”。
尽可能使用独立动画。应避免依赖动画,并且默认情况下将其禁用。如果您真的想对Height属性设置动画,则需要将EnableDependentAnimation属性设置为true。
此方案(高度与RenderTransform比例)是Make animations smooth (XAML)文档中使用的示例。
Blend也可以很好地设置动画。这是一个简单的例子。 Blend更喜欢CompositeTransform,因为它更灵活。如果知道只需要缩放,则可以使用ScaleTransform。如果希望它从顶部而不是中间打开,则可以将RenderTransformOrigin更改为0.5,0.0。
<Page.Resources>
<Storyboard x:Name="OpenSettings">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)" Storyboard.TargetName="SettingGrid">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="SettingGrid">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Page.Resources>
<Grid x:Name="SettingGrid" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<CompositeTransform/>
</Grid.RenderTransform>
</Grid>