WPF 如何实现先执行完动画再关闭窗口
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 如何实现先执行完动画再关闭窗口相关的知识,希望对你有一定的参考价值。
在XAML中 实现点击关闭按钮窗体,调用触发动画。
<!--关闭时触发窗体反转-->
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard Storyboard="StaticResource WindowRotation"/>
</EventTrigger>
在后台关闭事件
private void CloseWindow_Click(object sender, RoutedEventArgs e)
this.Close();
想实现点击按钮,窗体开始反转一次,然后窗体关闭。 现在问题是如果加了 this.Close(); 窗体直接关闭,没有动画,如果不加,那么有动画,但是不会关闭窗体。
求助,感谢,希望有关键代码或实例
动画效果我都实现了,只要问如何让动画结束才关闭窗体
教一个比较笨的方法,再CLOSE事件里面不写THIS.CLOSE(),先写e.Cancle=true;再定义一个时钟,时钟的INTERVAL=动画要执行的时间。然后close事件里面开启时钟。而时钟事件里面写上THIS.CLOSE();跟timer1.enable=flase;
那么执行CLOSEWINDOW_CLICK的时候,就会间隔INTERVAL的时间,这时间让你执行动画,然后执行THIS.CLOSE();关闭窗口。
比较简单的方法吧?^_^追问
非常感谢!
这个方法想到过,不过很少用时钟,写代码的话要再学习下时钟,:(
其实我有把动画写在.cs里面实现执行完动画再关闭窗口:
Storyboard sb = new Storyboard(); //定义个故事板
.........//定义动画内容和效果
sb.Completed += (a, b) => this.Close(); ; //把this.Close(); 写到Completed后边,但是我不知道这句话怎么写到AXML里面,如果能写到AXML里面,就完美了。
sb.Begin();
有答案我再追加分^^
<Window.RenderTransform>
<RotateTransform x:Name="rotate"></RotateTransform>
</Window.RenderTransform>
后台动画完成后执行关闭,可用Completed事件
private void CloseWindow_Click(object sender, RoutedEventArgs e)
DoubleAnimation rotation = new DoubleAnimation(-360, TimeSpan.FromSeconds(0.2));
rotation.Completed += new EventHandler(rotation_Completed);
rotation.AutoReverse = false;
rotation.FillBehavior = FillBehavior.HoldEnd;
window.rotate.BeginAnimation(RotateTransform.AngleProperty, rotation);
void rotation_Completed(object sender, EventArgs e)
this.Close();
不知道这是你需要的不 参考技术B 一句话:用异步解决。
WPF编游戏系列 之七 动画效果
原文:WPF编游戏系列 之七 动画效果(2)上一篇已经对关闭窗口图标进行了动画效果处理,本篇将对窗口界面的显示和关闭效果进行处理。由于所有的动画效果都是针对窗口界面的Canvas,所以先要为它添加一些RenderTranform属性,这些属性不要填写任何效果,后面会由Storyboard和EventTrigger为其提供。
<Canvas Name="queryCanvas" Visibility="Collapsed" RenderTransformOrigin="0.5,0.5"> <!-- TransformGroup都定义在这里 -->
<Canvas.RenderTransform> <TransformGroup> <ScaleTransform></ScaleTransform> <RotateTransform></RotateTransform> </TransformGroup> </Canvas.RenderTransform>
<!-- 下面是窗口界面代码 --> <Border Name="queryBorder" BorderThickness="9" CornerRadius="9"
Width="920" Height="440" Canvas.Left="40" Canvas.Top="160"> <ScrollViewer Name="queryScrollViewer"
ScrollViewer.VerticalScrollBarVisibility="Visible"> <StackPanel Orientation="Vertical">
<!-- 我就是那个关闭窗口图标,你不认识我啦! --> <Image Source="image/close.png" Name="closeImage" Height="20" Width="20" Cursor="Hand" Margin="5" HorizontalAlignment="Right"> <Image.ToolTip>Close</Image.ToolTip> </Image> <Grid Name="queryGrid"></Grid> </StackPanel> </ScrollViewer> </Border> </Canvas>
1. 下面就要在Window.Resources中为显示和关闭效果添加Storyboard了,其中showQueryCanvas就是显示效果,closeQueryCanvas是关闭效果。另,程序中涉及到的RenderTransform.Children[0]和[1],就是上面Resources里的ScaleTransform和RotataTransform。
<Window.Resources> ... ...
<Storyboard x:Key="showQueryCanvas"> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="Opacity" From="0.2" To="1" Duration="0:0:2.5">
</DoubleAnimation> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="RenderTransform.Children[1].Angle" From="70" To="0" Duration="0:0:2" >
</DoubleAnimation> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX" From="0" To="1" Duration="0:0:2"
AccelerationRatio="1">
</DoubleAnimation> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY" From="0" To="1" Duration="0:0:2"
AccelerationRatio="1">
</DoubleAnimation> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX" To="0.98" BeginTime="0:0:2" Duration="0:0:0.05"
DecelerationRatio="1">
</DoubleAnimation> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY" To="0.98" BeginTime="0:0:2" Duration="0:0:0.05"
DecelerationRatio="1">
</DoubleAnimation> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX" To="1" BeginTime="0:0:2.05" Duration="0:0:0.2"
AccelerationRatio="1">
</DoubleAnimation> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY" To="1" BeginTime="0:0:2.05" Duration="0:0:0.2"
AccelerationRatio="1">
</DoubleAnimation> </Storyboard> <Storyboard x:Key="closeQueryCanvas"> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="RenderTransform.Children[1].Angle" To="360" Duration="0:0:1.5" >
</DoubleAnimation> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:3">
</DoubleAnimation> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX" To="0" Duration="0:0:1.5" AccelerationRatio="1">
</DoubleAnimation> <DoubleAnimation Storyboard.TargetName="queryCanvas" Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY" To="0" Duration="0:0:1.5" AccelerationRatio="1">
</DoubleAnimation> </Storyboard> </Window.Resources>
2. 效果Over,就要启动事件了。关闭事件已经在上一篇写到关闭窗口图标事件中了,本篇只介绍显示效果事件。显示事件也同样定义到Window.Triggers中。下面程序中myHome、myShop、upgradeShop、myBank为程序上方四个图标的Name值。
<Window.Triggers> <!-- 关闭窗口图标事件就写在这里 --> ... ...
<EventTrigger SourceName="closeImage" RoutedEvent="Image.MouseLeftButtonDown"> <BeginStoryboard Name="closeQueryCanvasStoryboard"
Storyboard="{StaticResource closeQueryCanvas}">
</BeginStoryboard> </EventTrigger>
<!-- 下面是显示窗口事件 --> <EventTrigger SourceName="myHome" RoutedEvent="Image.MouseLeftButtonDown"> <BeginStoryboard Storyboard="{StaticResource showQueryCanvas}"></BeginStoryboard> </EventTrigger> <EventTrigger SourceName="myShop" RoutedEvent="Image.MouseLeftButtonDown"> <BeginStoryboard Storyboard="{StaticResource showQueryCanvas}"></BeginStoryboard> </EventTrigger> <EventTrigger SourceName="upgradeShop" RoutedEvent="Image.MouseLeftButtonDown"> <BeginStoryboard Storyboard="{StaticResource showQueryCanvas}"></BeginStoryboard> </EventTrigger> <EventTrigger SourceName="myBank" RoutedEvent="Image.MouseLeftButtonDown"> <BeginStoryboard Storyboard="{StaticResource showQueryCanvas}"></BeginStoryboard> </EventTrigger> </Window.Triggers>
3. 显示窗口效果
4. 关闭窗口效果
待续 … …
以上是关于WPF 如何实现先执行完动画再关闭窗口的主要内容,如果未能解决你的问题,请参考以下文章