与窗口大小有关的WPF波动/抖动动画

时间:2018-09-17 15:03:57

标签: wpf wpf-animation

我正在尝试为WPF应用程序创建一个滑出菜单,但是我注意到动画会变得抖动/缓慢,具体取决于屏幕的大小。我尝试使用Google搜索,但还没有真正找到任何解决方案,甚至没有人提到屏幕尺寸。

这里是我的XAML代码:

<Grid x:Name="sideMenu"  Visibility="Collapsed" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Grid.RowSpan="2" Background="White" >
 <Grid.RowDefinitions>
     <RowDefinition Height="50"/>
     <RowDefinition Height="*"/>
 </Grid.RowDefinitions>

 <Grid.ColumnDefinitions>
     <ColumnDefinition Width="*"/>
 </Grid.ColumnDefinitions>

 <Grid.RenderTransform>
     <TranslateTransform X="350" Y="0"/>
 </Grid.RenderTransform>

 <Border Grid.ColumnSpan="2" Grid.RowSpan="4" BorderBrush="Black" BorderThickness="1 0 0 0"></Border>
 <Border Background="#253031" BorderBrush="Black" Grid.ColumnSpan="2" BorderThickness="0 0 0 1"></Border>
 <Label Content="Menu Heading"  Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="White" FontSize="18" Margin="5" />
</Grid>

这也是我以下的故事板动画

SlideIn:

<Storyboard x:Key="SlideIn" Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)">
        <DoubleAnimation To="0" From="350"  Duration="0:0:0.2"/
</Storyboard>

SlideOut:

<Storyboard x:Key="SlideOut" Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)">
        <DoubleAnimation To="350" From="0"  Duration="0:0:0.2"/
</Storyboard>

最后,这是激活storboard动画的背后代码:

private void ShowMenu()
{
        //Start the slide animation
        Storyboard animation = Resources["SlideIn"] as Storyboard;
        animation.Begin(sideMenu);
}

private void HideMenu()
{
  Storyboard animation = Resources["SlideOut"] as Storyboard;
  animation.Begin(sideMenu);
}

任何想法或建议将不胜感激!

0 个答案:

没有答案