如何停止仅在XAML WPF组成的故事板 [英] How to stop WPF storyboard composed only in XAML

查看:206
本文介绍了如何停止仅在XAML WPF组成的故事板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XAML定义(作为用户控件),一个动画,基本上两个图像之间进行切换每秒:

I have a animation defined in XAML (as a UserControl) that essentially switches between two images every second:

    <UserControl x:Class="KaleidoscopeApplication.Controls.RemoteAnimation"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Loaded="RemoteAnimation_Loaded"
    Unloaded="RemoteAnimation_Unloaded">

    <Grid Canvas.Left="500" Canvas.Top="84">
        <Grid.Triggers>
            <EventTrigger RoutedEvent="Grid.Loaded">
                <BeginStoryboard>
                    <Storyboard x:Name="storyboard"  RepeatBehavior="Forever">
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="remote2" BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.Visibility)">
                            <DiscreteObjectKeyFrame KeyTime="0:0:1">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Collapsed</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                            <DiscreteObjectKeyFrame KeyTime="0:0:2">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Visible</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Grid.Triggers>

        <Image Name="remote1" Source="/Resources/Elements/Images/341.png"/>
        <Image Name="remote2" Source="/Resources/Elements/Images/342.png"/>

    </Grid>

</UserControl>

它可以在正是如此一个窗口被使用:

It can be used in a window thusly:

<!-- Remote -->
<uControl:RemoteAnimation 
     x:Name="remoteAnimation"
     Canvas.Left="316" Canvas.Top="156" Height="246" Width="121" />

我的问题是,当含有动画的窗口关闭,它使上运行,并导致泄漏。我无法通过RemoteAnimation_Unloaded()与storyboard.Stop()停止动画......它不会做插孔。

My problem is that when the window containing the animation closes, it keeps on running and causes a leak. I'm not able to stop the animation via RemoteAnimation_Unloaded() with storyboard.Stop()... it doesn't do jack.

我已经签出这两个职位,但它们并不适用:

I've checked out both of those posts, but they don't apply:

<一个href=\"http://stackoverflow.com/questions/1499477/wpf-window-close-not-triggering-usercontrol-unloaded-event\">Post1
POST2

我能够进入卸载方法,但调用停止()不停止动画。从我的理解,它可能是一个问题,通过调用开始()的故事板。有与isControlable参数的重载。然而,由于动画是完全在XAML,我不知道如何影响这一点。

I am able to get into the unloaded method, but calling Stop() does not stop the animation. From my understanding, it may be an issue with a call to Begin() for the storyboard. There is an overload with an isControlable parameter. However, since the animation is completely in XAML, I'm not sure how to affect this.

推荐答案

看起来像我跑成两个独立的问题:

Looks like I was running into two separate issues:

首先,在.NET 3.5,故事情节的动画可以泄漏非托管内存(啊):
<一href=\"http://connect.microsoft.com/VisualStudio/feedback/details/338723/wpf-storyboard-animations-leak-unmanaged-memory\">Link, <一href=\"http://stackoverflow.com/questions/1705849/wpf-memory-leak-on-xp-cmilchannel-hwnd\">Link

First off, in .NET 3.5, storyboard animations can leak unmanaged memory (ugh): Link, Link

由于我没有更新我的目标,以.NET 4.0的选项,我用的链接描述的补丁,它已经停止泄漏。

Since I don't have the option to update my targets to .NET 4.0, I used the patch described in the links and it has stopped the leak.

二,我是能够成功地挂钩到我的用户的空载情况下,这是当它包含窗口关闭调用。我看到别人有麻烦与此事件触发正确的,但它似乎为我工作。停止动画(这是通过XAML开始的repeatBehavior 的永远)的唯一方法是:

Second, I was able to successfully hook up to my UserControl's Unloaded event, which is called when it's containing window is closed. I see others have had trouble with this event firing properly, but it seems to work for me. The only way to stop the animation (which was started via XAML with RepeatBehavior of "Forever") is:

storyboard.Begin(this, true);
storyboard.Stop(this);

这停止播放动画,让GC收集它。

This stops the animation and allows the GC to collect it.

这篇关于如何停止仅在XAML WPF组成的故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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