关闭时淡出wpf窗口

时间:2009-05-15 09:00:22

标签: c# wpf animation eventtrigger routedevent

我想在我的应用程序中淡入/淡出窗口。
淡出时出现在Window.Loaded上,我希望在关闭时淡出(Window.ClosedWindow.Closing)。 淡入效果很好,但Window.Closing属性不允许RoutedEvent 我应该使用什么RoutedEvent来关闭?

    <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:2" FillBehavior="HoldEnd" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="Window.Closing">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:2" FillBehavior="HoldEnd" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>

我收到错误,值'Window.Closing'无法分配给属性'RoutedEvent'。事件名称无效。

5 个答案:

答案 0 :(得分:30)

Closing不是路由事件,因此您无法在EventTrigger中使用它。也许你可以在代码隐藏中的ClosingEvent的处理程序中启动故事板并取消事件......就像这样:

private bool closeStoryBoardCompleted = false;

private void Window_Closing(object sender, CancelEventArgs e)
{
    if (!closeStoryBoardCompleted)
    {
        closeStoryBoard.Begin();
        e.Cancel = true;
    }
}

private void closeStoryBoard_Completed(object sender, EventArgs e)
{
    closeStoryBoardCompleted = true;
    this.Close();
}

答案 1 :(得分:8)

我想我会添加另一种解决方案,使用Expression SDK中的行为并将其与@Thomas的解决方案相结合。使用它,我们可以定义一个“CloseBehavior”来处理启动故事板并在完成后关闭窗口的代码。

using System.ComponentModel;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;

namespace Presentation.Behaviours {
    public class CloseBehavior : Behavior<Window> {
        public static readonly DependencyProperty StoryboardProperty =
            DependencyProperty.Register("Storyboard", typeof(Storyboard), typeof(CloseBehavior), new PropertyMetadata(default(Storyboard)));

        public Storyboard Storyboard {
            get { return (Storyboard)GetValue(StoryboardProperty); }
            set { SetValue(StoryboardProperty, value); }
        }

        protected override void OnAttached() {
            base.OnAttached();
            AssociatedObject.Closing += onWindowClosing;
        }

        private void onWindowClosing(object sender, CancelEventArgs e) {
            if (Storyboard == null) {
                return;
            }
            e.Cancel = true;
            AssociatedObject.Closing -= onWindowClosing;

            Storyboard.Completed += (o, a) => AssociatedObject.Close();
            Storyboard.Begin(AssociatedObject);
        }
    }
}

该行为将故事板定义为依赖项属性,因此我们可以在xaml中设置它,当AssociatedObject(我们定义行为的窗口)关闭时,此故事板将使用Storyboard.Begin()启动。现在,在xaml中,我们只需使用以下xaml

将行为添加到窗口中
<Window x:Class="Presentation.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:behave="clr-namespace:Presentation.Behaviours"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        x:Name="window">
    <Window.Resources>
        <Storyboard x:Key="ExitAnimation">
            <DoubleAnimation Storyboard.Target="{Binding ElementName='window'}"
                             Storyboard.TargetProperty="(Window.Opacity)"
                             Duration="0:0:1" From="1" To="0"/>
        </Storyboard>
    </Window.Resources>

    <i:Interaction.Behaviors>
        <behave:CloseBehavior Storyboard="{StaticResource ExitAnimation}"/>
    </i:Interaction.Behaviors>

    <Grid>
    </Grid>
</Window>

请注意System.Windows.Interactivity dll中的xml名称空间i,并且还引用了该窗口,因此必须分配x:Name。现在,我们只需将行为添加到我们希望在关闭应用程序之前执行故事板的每个窗口,而不是将逻辑复制到每个窗口中的每个代码隐藏。

答案 2 :(得分:1)

我不是WPF的专家,但我相信除非您取消初始的Closing事件,否则窗口将在动画开始之前消失。

收到Window.Closing事件后,您应取消该事件并启动动画。动画完成后,您可以关闭窗口。

答案 3 :(得分:0)

这更简单,更短。添加如下行为:

public class WindowClosingBehavior : Behavior<Window>
    {
        protected override void OnAttached()
        {
            AssociatedObject.Closing += AssociatedObject_Closing;
        }

        private void AssociatedObject_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Window window = sender as Window;
            window.Closing -= AssociatedObject_Closing;
            e.Cancel = true;
            var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(0.5));
            anim.Completed += (s, _) => window.Close();
            window.BeginAnimation(UIElement.OpacityProperty, anim);
        }
        protected override void OnDetaching()
        {
            AssociatedObject.Closing -= AssociatedObject_Closing;
        }
    }

然后在你的窗口中添加一个引用:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:wt="clr-namespace:Desktop.Themes.WindowTask;assembly=Desktop.Themes"

插入行为:

<i:Interaction.Behaviors>
     <wt:WindowClosingBehavior />
</i:Interaction.Behaviors>

答案 4 :(得分:-2)

将AutoReverse设置为&#34; True&#34;

<Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" AutoReverse="True" From="0" To="1" Duration="0:0:0.5" FillBehavior="HoldEnd" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>
相关问题