淡出窗口
我目前正在开发一个 wpf c# 应用程序。我已将事件触发器添加到表单的 xaml 中,以便在窗口加载时淡入,在窗口关闭时淡出。
淡入效果完美,没有任何问题,但淡出效果不佳。
我对其进行了设置,以便窗口在加载时淡入,并有一个持续 5 秒的计时器,然后调用表单淡出事件。
但是,窗口不会淡出,它只是立即关闭,没有动画。以下是我用于淡入和淡出事件的代码,
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard Name="FormFade">
<DoubleAnimation Name="FormFadeAnimation"
Storyboard.TargetProperty="(Window.Opacity)"
From="0.0" To="1.0" Duration="0:0:1"
AutoReverse="False" RepeatBehavior="1x" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Window.Unloaded">
<BeginStoryboard>
<Storyboard Name="FormFadeOut" Completed="FormFadeOut_Completed">
<DoubleAnimation Name="FormFadeOutAnimation"
Storyboard.TargetName="FormFadeOut"
Storyboard.TargetProperty="(Window.Opacity)"
From="1.0" To="0.0" Duration="0:0:1"
AutoReverse="False" RepeatBehavior="1x" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
感谢您提供的任何帮助。
I am currently developing a wpf c# application. I have added to event triggers to the xaml of the form to fade in when the window loads and fades out when the window closes.
The fading in works perfectly without any problems but the fading out is not working.
I have it set up so the window fades in when it loads, has a timer to last 5 seconds, and then calls the form fade out event.
However, the window doesn't fade out it just closes straight away no animation. Below is the code I have for the fade in and fade out events
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard Name="FormFade">
<DoubleAnimation Name="FormFadeAnimation"
Storyboard.TargetProperty="(Window.Opacity)"
From="0.0" To="1.0" Duration="0:0:1"
AutoReverse="False" RepeatBehavior="1x" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Window.Unloaded">
<BeginStoryboard>
<Storyboard Name="FormFadeOut" Completed="FormFadeOut_Completed">
<DoubleAnimation Name="FormFadeOutAnimation"
Storyboard.TargetName="FormFadeOut"
Storyboard.TargetProperty="(Window.Opacity)"
From="1.0" To="0.0" Duration="0:0:1"
AutoReverse="False" RepeatBehavior="1x" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
Thanks for any help you can offer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Unloaded 不适合此事件,我不确定这个事件是否会发生在 Windows 上。您需要处理
Closing
,防止其实际关闭,启动动画并在动画的Completed
事件发生时关闭它。例如
Unloaded is not a suitable event for this, i'm not sure if this event can even occur for windows. You need to handle
Closing
, prevent it from actually closing, start an animation and close it when theCompleted
event of the animation occurs.e.g.
试试这个示例
just try this sample
HB 解决方案很好,但不能有效关闭窗口,因为 Close() 调用 window_Closing 并循环。这是我的工作解决方案:
H.B. solution is good but don't close effectively the window because Close() call window_Closing and loop. Here my working solution: