6

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

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:

Post1 Post2

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.

Community
  • 1
  • 1
BabaBooey
  • 1,552
  • 3
  • 21
  • 38

2 Answers2

12

Looks like I was running into two separate issues:

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

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.

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

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

Community
  • 1
  • 1
BabaBooey
  • 1,552
  • 3
  • 21
  • 38
  • 2
    Thanks, I was looking for a way to stop my "forever" animation but just calling Stop(this) didn't do it. Adding "true" to the Start(this) method did the trick. – Björn Nov 25 '11 at 08:14
  • This non obvious API design of the Begin method with 12 overloads has got me too. The default should be controllable since animations are by definition dynamic and are quite hard to do with a pure xaml appraoach. – Alois Kraus Jan 06 '14 at 07:17
1

Please read these two posts:

Unloaded event not triggered

Disposing UserControls

Community
  • 1
  • 1
Emond
  • 50,210
  • 11
  • 84
  • 115
  • I've checked out both of those, but they don't apply. 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. – BabaBooey Mar 22 '11 at 13:32
  • Why didn't you add that to the question? Please do so now. – Emond Mar 22 '11 at 13:34
  • No dice... The jerk just keeps running. Why can't it be nice and actually listen to its own Stop() method?! :) – BabaBooey Mar 22 '11 at 13:46
  • BTW: how do you know it keeps on running and causes a memory leak? – Emond Mar 22 '11 at 19:53