Пример #1
0
        private static void AnimatedSourceChanged(
            DependencyObject o,
            DependencyPropertyChangedEventArgs e)
        {
            if (!(o is Image imageControl))
            {
                return;
            }
            ImageSource oldValue = e.OldValue as ImageSource;
            ImageSource newValue = e.NewValue as ImageSource;

            if (oldValue == newValue)
            {
                return;
            }
            if (oldValue != null)
            {
                imageControl.Loaded   -= new RoutedEventHandler(ImageBehavior.ImageControlLoaded);
                imageControl.Unloaded -= new RoutedEventHandler(ImageBehavior.ImageControlUnloaded);
                AnimationCache.DecrementReferenceCount(oldValue, ImageBehavior.GetRepeatBehavior(imageControl));
                ImageBehavior.GetAnimationController(imageControl)?.Dispose();
                imageControl.Source = (ImageSource)null;
            }
            if (newValue == null)
            {
                return;
            }
            imageControl.Loaded   += new RoutedEventHandler(ImageBehavior.ImageControlLoaded);
            imageControl.Unloaded += new RoutedEventHandler(ImageBehavior.ImageControlUnloaded);
            if (!imageControl.IsLoaded)
            {
                return;
            }
            ImageBehavior.InitAnimationOrImage(imageControl);
        }
Пример #2
0
        private static void ImageControlUnloaded(object sender, RoutedEventArgs e)
        {
            if (!(sender is Image imageControl))
            {
                return;
            }
            ImageSource animatedSource = ImageBehavior.GetAnimatedSource(imageControl);

            if (animatedSource != null)
            {
                AnimationCache.DecrementReferenceCount(animatedSource, ImageBehavior.GetRepeatBehavior(imageControl));
            }
            ImageBehavior.GetAnimationController(imageControl)?.Dispose();
        }