淡出窗口 [英] Fading out a window

查看:116
本文介绍了淡出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个WPF C#应用程序。我已经添加到事件触发窗体的XAML淡出窗口时加载和淡出的时候窗口关闭。

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.

我有它设置,所以当它加载窗口淡入,有一个计时器,持续5秒,然后调用形式淡出的事件。

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.

但是,窗口不会淡出它只是关闭马上没有动画。下面是code我在淡入和淡出的事件

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.

推荐答案

<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.unloaded.aspx\">Unloaded是不是这个合适的情况下,我不知道,如果偶可发生于Windows此事件。你需要处理结束,prevent它实际上关闭,启动动画并关闭它已完成动画的事件发生。

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 the Completed event of the animation occurs.

例如

<Window ...
        Closing="Window_Closing">

private void Window_Closing(object sender, CancelEventArgs e)
{
    Closing -= Window_Closing;
    e.Cancel = true;
    var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(1));
    anim.Completed += (s, _) => this.Close();
    this.BeginAnimation(UIElement.OpacityProperty, anim);
}

这篇关于淡出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆