我在这里有两个动画,一个用于关闭滑出网格导航菜单,另一个用于在菜单关闭时将矩形填充设置为透明。
我希望这两者同时发生。现在,动画按照它们被调用的顺序发生。
如何使用C#代码尽可能简单,干净地实现它?我只是创建这个应用程序来了解动画和不同的布局控制方式。
private void _CloseSlideGrid()
{
DoubleAnimation da = new DoubleAnimation();
da.Duration = TimeSpan.FromSeconds(0.3d);
da.DecelerationRatio = 1.0d;
da.From = 500.0d;
da.To = 0.0d;
_slideGrid.BeginAnimation(Grid.WidthProperty, da);
}
private void _DisableTransparentCover()
{
BrushAnimation ba = new BrushAnimation();
ba.Duration = TimeSpan.FromSeconds(0.3d);
ba.DecelerationRatio = 1.0d;
ba.From = _GetBrush("#77000000");
ba.To = _GetBrush("#00000000");
_tranparentCover.BeginAnimation(Rectangle.FillProperty, ba);
}
我的关闭按钮的事件回调调用我将处理动画的两个私有函数。
private void _NavCloseButton_Click(object sender, RoutedEventArgs e)
{
_CloseSlideGrid();
_DisableTransparentCover();
}
以下是Imgur相册的链接,其中包含我的窗口两种状态的屏幕截图,如果您有兴趣: http://imgur.com/a/ZaSr1
如果我能提供更多信息,请告诉我,
感谢。
答案 0 :(得分:1)
只需将它们添加到同一个故事板上即可。我不确定你的BrushAnimation是什么,但使用ColorAnimation和属性路径如下工作正常。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
SizeToContent="WidthAndHeight">
<Window.Resources>
<Storyboard x:Key="CloseAnimation">
<DoubleAnimation From="500.0" To="0.0" DecelerationRatio="1.0" Duration="00:00:03"
Storyboard.TargetName="MyTextBox" Storyboard.TargetProperty="Width"/>
<ColorAnimation From="#77000000" To="#00000000" DecelerationRatio="1.0" Duration="00:00:03"
Storyboard.TargetName="MyGrid" Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"/>
</Storyboard>
</Window.Resources>
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox x:Name="MyTextBox" Grid.Column="0" Width="500"/>
<Grid x:Name="MyGrid" Grid.Column="1" Background="#77000000" Width="100"/>
</Grid>
<Button Content="Animate">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click" >
<BeginStoryboard Storyboard="{StaticResource CloseAnimation}"/>
</EventTrigger>
</Button.Triggers>
</Button>
</StackPanel>
</Window>
如果你真的想在后面的代码中执行此操作并且没有其他方法可以翻译它。
private void _CloseSlideGrid(Storyboard sb)
{
DoubleAnimation da = new DoubleAnimation();
da.Duration = TimeSpan.FromSeconds(0.3d);
da.DecelerationRatio = 1.0d;
da.From = 500.0d;
da.To = 0.0d;
Storyboard.SetTarget(da, MyTextBox);
Storyboard.SetTargetProperty(da, new PropertyPath("Width"));
sb.Children.Add(da);
}
private void _DisableTransparentCover(Storyboard sb)
{
ColorAnimation ba = new ColorAnimation();
ba.Duration = TimeSpan.FromSeconds(0.3d);
ba.DecelerationRatio = 1.0d;
ba.From = (Color)ColorConverter.ConvertFromString("#77000000");
ba.To = (Color)ColorConverter.ConvertFromString("#00000000");
Storyboard.SetTarget(ba, MyGrid);
Storyboard.SetTargetProperty(ba, new PropertyPath("(Background).(SolidColorBrush.Color)"));
sb.Children.Add(ba);
}
private void _NavCloseButton_Click(object sender, RoutedEventArgs e)
{
var sb = new Storyboard();
_CloseSlideGrid(sb);
_DisableTransparentCover(sb);
sb.Begin();
}