我有一个在XAML中定义的动画(作为UserControl),它每秒钟交替显示两张图片:
我的问题是,当包含动画的窗口关闭时,它仍在运行并导致泄漏。我无法通过RemoteAnimation_Unloaded()和storyboard.Stop()停止动画... 它没有任何作用。
我已经查看了这两篇文章,但它们并不适用: Post1 Post2 我能够进入未加载方法,但调用Stop()不能停止动画。据我所知,可能与对storyboard的Begin()调用有关。有一个带有isControlable参数的重载。然而,由于动画完全在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 我能够进入未加载方法,但调用Stop()不能停止动画。据我所知,可能与对storyboard的Begin()调用有关。有一个带有isControlable参数的重载。然而,由于动画完全在XAML中,我不知道如何影响它。