private static void AnimatedSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { Image image = o as Image; if (image != null) { ImageSource oldValue = e.OldValue as ImageSource; ImageSource newValue = e.NewValue as ImageSource; if (oldValue != null) { image.Loaded -= new RoutedEventHandler(ImageBehavior.ImageControlLoaded); image.Unloaded -= new RoutedEventHandler(ImageBehavior.ImageControlUnloaded); AnimationCache.DecrementReferenceCount(oldValue, GetRepeatBehavior(image)); ImageAnimationController animationController = GetAnimationController(image); if (animationController != null) { animationController.Dispose(); } image.Source = null; } if (newValue != null) { image.Loaded += new RoutedEventHandler(ImageBehavior.ImageControlLoaded); image.Unloaded += new RoutedEventHandler(ImageBehavior.ImageControlUnloaded); if (image.IsLoaded) { InitAnimationOrImage(image); } } } }
private static void InitAnimationOrImage(Image imageControl) { ImageAnimationController animationController = GetAnimationController(imageControl); if (animationController != null) { animationController.Dispose(); } SetAnimationController(imageControl, null); SetIsAnimationLoaded(imageControl, false); BitmapSource source = GetAnimatedSource(imageControl) as BitmapSource; bool animateInDesignMode = GetAnimateInDesignMode(imageControl); bool flag3 = !DesignerProperties.GetIsInDesignMode(imageControl) || animateInDesignMode; bool flag4 = IsLoadingDeferred(source); if ((source != null) && (flag3 && !flag4)) { Action action = null; if (source.IsDownloading) { EventHandler handler = null; handler = delegate(object sender, EventArgs args) { source.DownloadCompleted -= handler; InitAnimationOrImage(imageControl); }; source.DownloadCompleted += handler; imageControl.Source = source; return; } ObjectAnimationUsingKeyFrames animation = GetAnimation(imageControl, source); if (animation != null) { if (animation.KeyFrames.Count <= 0) { imageControl.Source = source; } else { if (action == null) { action = () => imageControl.Source = (ImageSource)animation.KeyFrames[0].Value; } TryTwice(action); } animationController = new ImageAnimationController(imageControl, animation, GetAutoStart(imageControl)); SetAnimationController(imageControl, animationController); SetIsAnimationLoaded(imageControl, true); imageControl.RaiseEvent(new RoutedEventArgs(AnimationLoadedEvent, imageControl)); return; } } imageControl.Source = source; if (source != null) { SetIsAnimationLoaded(imageControl, true); imageControl.RaiseEvent(new RoutedEventArgs(AnimationLoadedEvent, imageControl)); } }
private static void ImageControlUnloaded(object sender, RoutedEventArgs e) { Image image = sender as Image; if (image != null) { ImageSource animatedSource = GetAnimatedSource(image); if (animatedSource != null) { AnimationCache.DecrementReferenceCount(animatedSource, GetRepeatBehavior(image)); } ImageAnimationController animationController = GetAnimationController(image); if (animationController != null) { animationController.Dispose(); } } }