WPF Window.Loaded 和 Window.Closed

发布于 2024-10-27 04:34:23 字数 1014 浏览 1 评论 0原文

有两个事件触发器。其中之一是 RoundedEvent Window.Loaded ,另一个是 Window.Closed。 但两者不能同时工作。我该如何解决这个问题?

我的代码如下

<Window.Triggers>
    <EventTrigger RoutedEvent="Window.Closed">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation  Storyboard.TargetName="mainWindow" 
        Storyboard.TargetProperty="Left"
        From="500" To="-200" Duration="0:0:1" >
                </DoubleAnimation>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>

    <EventTrigger RoutedEvent="Window.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
        Storyboard.TargetName="mainWindow" 
        Storyboard.TargetProperty="Left"
        From="-200" To="500" Duration="0:0:1" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers>

There are two event triggers. One of them is RoundedEvent Window.Loaded and the the other Window.Closed.
But both of them does not work at the same time. How can I fix The problem?

My code is below

<Window.Triggers>
    <EventTrigger RoutedEvent="Window.Closed">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation  Storyboard.TargetName="mainWindow" 
        Storyboard.TargetProperty="Left"
        From="500" To="-200" Duration="0:0:1" >
                </DoubleAnimation>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>

    <EventTrigger RoutedEvent="Window.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
        Storyboard.TargetName="mainWindow" 
        Storyboard.TargetProperty="Left"
        From="-200" To="500" Duration="0:0:1" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

层林尽染 2024-11-03 04:34:23

Window.Loaded 工作正常,问题是 EventTriggers 仅适用于路由事件,但 Window.Closed 不是路由事件(因为它是某种东西)这只能发生在一个窗口上)。

我建议您通过处理 Window.Closed 事件来在代码隐藏中执行关闭动画,无论如何它都是有意义的,因为您需要等待动画结束才能真正关闭窗口。

Window.Loaded works fine, the problem is that EventTriggers only work for routed events but Window.Closed is not routed event (since it is something that can only occur to a window).

I suggest you do the closing animation in code-behind by handling the Window.Closed event, it makes sense anyway because you need to wait for the animation to end before you actually close the window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文