【问题标题】:How to stop WPF storyboard composed only in XAML如何停止仅在 XAML 中组成的 WPF 故事板
【发布时间】:2011-03-22 13:14:54
【问题描述】:

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

    <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>

因此可以在窗口中使用:

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

我的问题是当包含动画的窗口关闭时,它会继续运行并导致泄漏。我无法通过 RemoteAnimation_Unloaded() 和 storyboard.Stop() 停止动画...它不会做杰克。

我已经查看了这两个帖子,但它们都不适用:

Post1 Post2

我能够进入 unloaded 方法,但调用 Stop() 不会停止动画。据我了解,调用故事板的 Begin() 可能是一个问题。 isControlable 参数存在重载。但是,由于动画完全在 XAML 中,我不确定如何影响这一点。

【问题讨论】:

    标签: wpf xaml animation user-controls storyboard


    【解决方案1】:

    看起来我遇到了两个不同的问题:

    首先,在 .NET 3.5 中,情节提要动画可能会泄漏非托管内存(呃): Link, Link

    由于我没有将目标更新到 .NET 4.0 的选项,因此我使用了链接中描述的补丁,它已经阻止了泄漏。

    其次,我能够成功连接到我的 UserControl 的 Unloaded 事件,该事件在它包含的窗口关闭时被调用。我看到其他人在正确触发此事件时遇到了麻烦,但它似乎对我有用。停止动画(通过 XAML 以“Forever”的RepeatBehavior 启动)的唯一方法是:

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

    这会停止动画并允许 GC 收集它。

    【讨论】:

    • 谢谢,我正在寻找一种方法来停止我的“永远”动画,但只是调用 Stop(this) 并没有做到。在 Start(this) 方法中添加“true”就可以了。
    • Begin 方法的这种不明显的 API 设计具有 12 个重载,也让我着迷。默认值应该是可控的,因为动画根据定义是动态的,并且很难用纯 xaml 方法来完成。
    【解决方案2】:

    【讨论】:

    • 我已经检查了这两个,但它们不适用。我能够进入卸载的方法,但调用 Stop() 不会停止动画。据我了解,调用故事板的 Begin() 可能是个问题。 isControlable 参数存在重载。但是,由于动画完全在 XAML 中,我不确定如何影响这一点。
    • 您为什么不将其添加到问题中?请现在就这样做。
    • 没有骰子...混蛋只是继续跑。为什么不能很好听,实际听听自己的 Stop() 方法?! :)
    • 顺便说一句:你怎么知道它继续运行并导致内存泄漏?
    猜你喜欢
    • 2021-02-16
    • 2017-02-09
    • 2014-01-18
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    相关资源
    最近更新 更多